Run AppleScript From Terminal With osascript
Shell scripts are great at files and text, and helpless at "pause the music" or "put a notification on screen." AppleScript is the opposite. osascript joins them: it runs any AppleScript from the Terminal, one line or a whole file, so your shell scripts can talk to apps and your Mac can talk back.
One-liners worth knowing
The -e flag runs a line of AppleScript directly. Three immediately useful examples:
# put a notification on screen (great at the end of long scripts)
osascript -e 'display notification "Backup finished" with title "Script"'
# ask a yes/no question, get the answer back
osascript -e 'display dialog "Continue with cleanup?"'
# set the output volume (0-100)
osascript -e 'set volume output volume 25'
The dialog command exits with an error if the user clicks Cancel, which makes it a working confirmation gate in shell scripts: osascript -e 'display dialog "Proceed?"' && ./do-the-thing.sh. The volume change is undone the same way you made it — run it again with the old value.
Controlling apps with tell
AppleScript's verb for addressing an app is tell. Anything a scriptable app exposes, you can trigger from Terminal:
# play/pause Music without touching the app
osascript -e 'tell application "Music" to playpause'
# open a new Finder window at your home folder
osascript -e 'tell application "Finder" to open home'
# empty the Trash (same as Finder's own command)
osascript -e 'tell application "Finder" to empty trash'
The first time a Terminal script controls a given app, macOS shows a permission prompt — "Terminal" wants access to control "Music". Click OK once and it's remembered; you can review or revoke these grants any time in System Settings → Privacy & Security → Automation. If you clicked Don't Allow and regret it, that same pane is the undo: flip the toggle for Terminal back on.
Multi-line scripts and script files
Stack -e flags and they run as consecutive lines:
osascript -e 'tell application "Safari"' \
-e 'make new document' \
-e 'end tell'
For anything longer, put the AppleScript in a file and run osascript myscript.applescript — same engine, no quoting headaches. A heredoc works too when you want the script inline in a shell script:
osascript <<'END'
tell application "Finder"
set trashCount to count of items in trash
end tell
display notification (trashCount as text) & " items in Trash"
END
Whatever the last expression returns is printed to standard output — so result=$(osascript -e '…') passes values from the app world back into your shell variables. That round trip is what makes osascript more than a novelty.
JXA: the same bridge in JavaScript
If AppleScript's English-like syntax grates, the same automation engine speaks JavaScript. Add -l JavaScript:
# JavaScript for Automation (JXA)
osascript -l JavaScript -e 'Application("Music").playpause()'
JXA reaches the same apps with the same permissions model; use whichever language you'd rather debug. One boundary to know about either way: scripts that click buttons or press keys in other apps (via System Events UI scripting) additionally need Terminal listed under Privacy & Security → Accessibility — plain app commands like the ones above don't.
Dialogs can do more than OK/Cancel, which turns them into real script interfaces:
# a custom two-button choice, reading back the answer
osascript -e 'button returned of (display dialog "Deploy now?" buttons {"Later", "Ship it"} default button "Ship it")'
That prints Ship it or Later to standard output, so a shell case statement can branch on a genuinely graphical question — the missing piece for scripts a less technical colleague has to run.
Plenty of 'automation' is really just settings you wish were reachable. Mainspring makes 90+ hidden macOS settings into labelled, reversible toggles — no scripting session required.
Try Mainspring free →Signed & notarized by Apple · 1-day free trial · $29 once
When Shortcuts is the better bridge
For automations you'd rather build visually and still trigger from scripts, the Shortcuts app has its own command-line runner — see our guide to running Shortcuts from Terminal.