Run JavaScript for Automation (JXA) from the Terminal

Execute a one-line JavaScript automation against Mac apps using osascript -l JavaScript.

macOSosascriptautomationscriptingLow risk
Run osascript -l JavaScript -e 'Application("Music").playpause()' to toggle Music playback — any app with scripting support works the same way.

01Command

Command
$ osascript -l JavaScript -e 'Application("Music").playpause(); 1+1'

02What it does

macOS ships JavaScript for Automation (JXA) as an alternative OSA language to AppleScript; -l JavaScript switches osascript into that mode. You get real JS syntax plus the Application(), ObjC and bridging objects, which is much friendlier for JSON handling and string manipulation than AppleScript. Omit -e and osascript reads the script from stdin or a file.

03Risk

RiskLow risk
!Warning: JXA scripting of third-party apps needs the same Automation permissions as AppleScript — expect a consent prompt on first use, and note Apple's documentation increasingly favors Swift/Shortcuts for new automation.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • osascript(1) man page
    manpagez.com · technical · 2026-09-17
    Evidence: The osascript synopsis shows [-l language] [-e statement] and explains that language selects among installed OSA languages, including JavaScript.
  • osascript - Execute OSA Scripts
    SS64 · technical · 2026-09-17
    Evidence: Documents the -l language option which selects the OSA language for the script, and -e statement to run one line from the command line.

Last verified: 2026-09-19 · Category: Developer