Skip to Content
Back to Actions

Updated

How to Run JavaScript in Shortcuts on iPhone and Mac

With Actions

  1. Install Actions, and open it once.
  2. In your shortcut, add the “Transform Text with JavaScript” action of Actions.
  3. In the first field, choose the text, like the result of the action above it.
  4. In the second field, write the code. The text is in the $text variable, and the code must return a string.

For example, this code keeps only the lines that have an email address:

js
return $text.split('\n').filter(line => line.includes('@')).join('\n');

The result is “Transformed Text”. To return an object, return it as JSON with JSON.stringify(), and give the result to the “Get Dictionary from Input” action of Shortcuts.

The code runs in JavaScriptCore, the JavaScript engine of Safari, and without a web page. So it has the JavaScript language, like JSON, Math, and regular expressions, but not the things of a browser, like document and fetch. The code must be synchronous: it cannot wait for a promise.

Get Actions

Additional actions for the Shortcuts app

Download on the App Store

Free

Platforms
macOS, iOS, visionOS
Requires
macOS, iOS, or visionOS 26 or later
Category
Utilities
Released
October 2021
Updated
June 2026
Version
4.2.0
Size
29 MB
US App Store rating
4.8/5 (529 ratings)

More about Actions

Without an app

On a web page: The “Run JavaScript on Web Page” action runs your code on the page that is open in Safari, like to get data from the page, or to change it. On iPhone and iPad, turn on “Show in Share Sheet” in the details of the shortcut, and run the shortcut from the share sheet of Safari. Before the first run, you must turn on “Allow Running Scripts” in the settings of Shortcuts. Apple’s instructions.

On Mac: The “Run JavaScript for Mac Automation” action runs JavaScript that controls the apps of your Mac. It is not on iPhone or iPad.

These actions are the right tools for a web page, or to control Mac apps. To only calculate something or change a text, the action of Actions is simpler, and the same shortcut works on iPhone, iPad, and Mac.