Browse the docs

Run JXA script

Runs JavaScript for Automation with osascript to control Mac apps.

Run JXA script

Keyboard Maestro: ExecuteJavaScriptForAutomation

Details #

Runs script as JavaScript for Automation (osascript -l JavaScript). This is the same engine as Run AppleScript, with JavaScript syntax, so it can script apps with Application("Finder"). Whatever the script returns as its last value is printed and becomes the output text. If into is set, that variable gets it.

The output is an object with stdout, exit and, if any, stderr. A script error fails the step with the first 300 characters of the message.

This step is not Run JavaScript. That one runs inside Switchboard with no access to apps, files or the network; JXA runs outside and can do anything your user can.

Parameters #

ParameterTypeRequiredValuesWhat it does
scripttextYesRequired. The JXA source.
argstextText handed to the script as its arguments, one per line (or a list). JavaScript gets them as the argument of function run(argv).
intotextThe variable that receives the result text.
sampleany valueWhat into holds during a dry run.
timeoutnumberSeconds before the step is cancelled.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Get the front app's name via JXA (runs a script)
Do
  1. Run JXA script “Application('System Events').processes.…”
  2. Show “Front app: {{front}}”
Open in Switchboard to try it

Gotchas #

  • Dry run. The script does not run; into gets sample (or an empty string).

  • Timeout. Cancelled after 10 s by default; set timeout for longer jobs.

  • Permissions. Controlling another app triggers a macOS Automation prompt, same as AppleScript. Fix refusals in System Settings › Privacy & Security › Automation.

  • Do not store secrets in the script text.

See also #

Run AppleScript, Run JavaScript