3

I am trying the “Run JavaScript on Web Page” action in Apple Shortcuts with this simple script:

alert("test"); completion(result); 

The alert fires and shows a message. However, I keep getting this error box at the end:

“Unable to Run JavaScript on Web Page. Make sure that ‘Allow JavaScript from Apple Events’ is enabled in the Develop menu in Safari. The Develop menu can be enabled in the Advanced section of Safari’s Preferences.”

I’ve already verified and tried:

  1. JavaScript is enabled in Safari.
  2. The Develop menu is activated.
  3. “Allow JavaScript from Apple Events” is checked.
  4. Restarted both apps
  5. Rebooted
  6. Ran defaults write -app Safari AllowJavaScriptFromAppleEvents 1

What could be causing this issue, and how can I resolve it?

1 Answer 1

4

There is a syntax error in the JavaScript code. Specifically, the result variable that is being passed to completion() is undefined. This would throw an error, causing the shortcut to fail.

Fixed code:

var result = []; // Declare the result variable alert("test"); completion(result); // Pass the result to completion() 
2
  • Technically this isn't a syntax error. It's either a ReferenceError or a type error: Apple's docs suggest it's not a type error, since "If you don’t want to return any data from the Run JavaScript on Webpage action, you can call completion() with no argument in the function (because undefined is a valid output). This is identical to calling completion(undefined).". Just the declaration var result; should be enough to fix the error, if my theory is correct. Commented Nov 27, 2024 at 23:51
  • Calling completion(), without any arguments, indeed resolves the problem. Commented Nov 28, 2024 at 0:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.