Run AppleScript and OSA JavaScript from Hammerspoon scripts.
Script execution is isolated in a separate XPC helper process
(HammerspoonOSAScriptHelper). If a script crashes or deadlocks, only the
helper is affected — the main app remains stable and the next call
reconnects automatically.
Async API (Promise-based)
Every async function returns a Promise that always resolves (never rejects)
with an HSOSAScriptResult: whether the script succeeded, its parsed result, and its raw result
or error message.
Parallel execution
Async scripts run in parallel, and their Promises resolve in whatever
order the scripts finish, not the order they were started. A slow script
(for example one waiting up to two minutes for an unresponsive application
to answer an AppleEvent) does not hold up any other script.
If your scripts must run in a strict order, chain them so that each one
only starts once the previous one has finished:
hs.osascript.applescript('tell application "Music" to pause') .then(() =>hs.osascript.applescript('tell application "Music" to set sound volume to 50')) .then(() =>hs.osascript.applescript('tell application "Music" to play'));
or, inside an async function:
asyncfunctionrestartMusic() { awaiths.osascript.applescript('tell application "Music" to pause'); awaiths.osascript.applescript('tell application "Music" to set sound volume to 50'); awaiths.osascript.applescript('tell application "Music" to play'); }
No user interface
Scripts cannot show user interface of their own. The helper process runs in
the background, so Standard Additions commands that interact with the user,
such as display dialog, fail with the error "No user interaction allowed."
(success is false). Scripts can still tell
other applications to do things, including showing their own windows.
To interact with the user, use Hammerspoon's own APIs instead, such as
hs.ui.dialog(), hs.ui.alert(), hs.ui.textPrompt(), hs.ui.filePicker()
or hs.notify, and pass the results to your script if needed.
Sync API
The *Sync variants block until the script completes and return the same
{ success, result, raw } object directly.
Warning: while a sync call is running, Hammerspoon's main thread is
blocked. Nothing else in Hammerspoon can run: no timers, hotkeys, event
watchers, UI updates, or console. If the target application is slow to
respond, Hammerspoon hangs until it does, or until the AppleEvent times out
(two minutes by default). If the target application tries to talk back to
Hammerspoon while it is blocked, both wait on each other until that timeout.
Prefer the async API, and only use the *Sync variants for scripts that are
known to finish quickly.
The result field is typed based on what the script returned: strings,
numbers, booleans, lists, and records are all mapped to their JavaScript
equivalents. null is used for AppleScript's missing value and for any
failure case.
Run AppleScript and OSA JavaScript from Hammerspoon scripts.
Script execution is isolated in a separate XPC helper process (
HammerspoonOSAScriptHelper). If a script crashes or deadlocks, only the helper is affected — the main app remains stable and the next call reconnects automatically.Async API (Promise-based)
Every async function returns a
Promisethat always resolves (never rejects) with anHSOSAScriptResult: whether the script succeeded, its parsed result, and its raw result or error message.Parallel execution
Async scripts run in parallel, and their Promises resolve in whatever order the scripts finish, not the order they were started. A slow script (for example one waiting up to two minutes for an unresponsive application to answer an AppleEvent) does not hold up any other script.
If your scripts must run in a strict order, chain them so that each one only starts once the previous one has finished:
or, inside an
asyncfunction:No user interface
Scripts cannot show user interface of their own. The helper process runs in the background, so Standard Additions commands that interact with the user, such as
display dialog, fail with the error "No user interaction allowed." (successisfalse). Scripts can still tell other applications to do things, including showing their own windows.To interact with the user, use Hammerspoon's own APIs instead, such as
hs.ui.dialog(),hs.ui.alert(),hs.ui.textPrompt(),hs.ui.filePicker()orhs.notify, and pass the results to your script if needed.Sync API
The
*Syncvariants block until the script completes and return the same{ success, result, raw }object directly.Warning: while a sync call is running, Hammerspoon's main thread is blocked. Nothing else in Hammerspoon can run: no timers, hotkeys, event watchers, UI updates, or console. If the target application is slow to respond, Hammerspoon hangs until it does, or until the AppleEvent times out (two minutes by default). If the target application tries to talk back to Hammerspoon while it is blocked, both wait on each other until that timeout. Prefer the async API, and only use the
*Syncvariants for scripts that are known to finish quickly.The
resultfield is typed based on what the script returned: strings, numbers, booleans, lists, and records are all mapped to their JavaScript equivalents.nullis used for AppleScript'smissing valueand for any failure case.Examples
Return a string (async):
Return a string (sync):
Interact with an application:
Handle errors (the Promise never rejects — check
success):OSA JavaScript:
Run a script from a file: