hs.osascript
Module
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 object containing three fields:
| Field | Type | Description |
|---|---|---|
success |
Boolean |
true if the script ran without error |
result |
any |
Parsed return value of the script, or null on failure |
raw |
String |
Raw string representation of the result, or the error message on failure |
Sync API
The *Sync variants block until the script completes and return the same
{ success, result, raw } object directly. Use these only when a Promise
chain is impractical; they block the JS thread for the duration of the call.
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.
Examples
Return a string (async):
hs.osascript.applescript('return "hello"')
.then(r => console.log(r.result)); // "hello"
Return a string (sync):
const r = hs.osascript.applescriptSync('return "hello"');
console.log(r.result); // "hello"
Interact with an application:
hs.osascript.applescript('tell application "Finder" to get name of home')
.then(r => console.log(r.result)); // e.g. "cmsj"
Handle errors (the Promise never rejects — check success):
hs.osascript.applescript('this is not valid')
.then(r => {
if (!r.success) console.log("Error:", r.raw);
});
OSA JavaScript:
hs.osascript.javascript('Application("Finder").name()')
.then(r => console.log(r.result)); // "Finder"
Run a script from a file:
hs.osascript.applescriptFromFile('/Users/me/scripts/notify.applescript')
.then(r => console.log(r.success));
Types
This module does not provide any types.
Properties
This module has no properties.
Methods
hs.osascript.applescript
Run an AppleScript source string.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:84
Declaration
hs.osascript.applescript(source) -> Promise<any>
Parameters
-
sourcestringThe AppleScript source code to compile and execute.
Return Value
A `Promise` resolving to `{ success, result, raw }`.
hs.osascript.javascript
Run an OSA JavaScript source string. OSA JavaScript is Apple's Open Scripting Architecture dialect of JavaScript, distinct from the JavaScriptCore engine that runs Hammerspoon scripts themselves.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:94
Declaration
hs.osascript.javascript(source) -> Promise<any>
Parameters
-
sourcestringThe OSA JavaScript source code to compile and execute.
Return Value
A `Promise` resolving to `{ success, result, raw }`.
hs.osascript.applescriptFromFile
Read a file from disk and execute its contents as AppleScript. The file is read in the main process before being sent to the XPC helper. If the file cannot be read the promise resolves immediately with `{ success: false, result: null, raw: "Failed to read file: <path>" }`.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:104
Declaration
hs.osascript.applescriptFromFile(path) -> Promise<any>
Parameters
-
pathstringAbsolute path to the AppleScript source file.
Return Value
A `Promise` resolving to `{ success, result, raw }`.
hs.osascript.javascriptFromFile
Read a file from disk and execute its contents as OSA JavaScript. The file is read in the main process before being sent to the XPC helper. If the file cannot be read the promise resolves immediately with `{ success: false, result: null, raw: "Failed to read file: <path>" }`.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:114
Declaration
hs.osascript.javascriptFromFile(path) -> Promise<any>
Parameters
-
pathstringAbsolute path to the OSA JavaScript source file.
Return Value
A `Promise` resolving to `{ success, result, raw }`.
hs.osascript._execute
Low-level execution entry point used by the higher-level helpers. Prefer `applescript()` or `javascript()` over calling this directly.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:124
Declaration
hs.osascript._execute(source, language) -> Promise<any>
Parameters
-
sourcestringThe script source code.
-
languagestringThe OSA language name — must be `"AppleScript"` or `"JavaScript"`.
Return Value
A `Promise` resolving to `{ success, result, raw }`.
hs.osascript.applescriptSync
Run an AppleScript source string synchronously. Blocks the JS thread until the script completes.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:134
Declaration
hs.osascript.applescriptSync(source) -> [String: Any]
Parameters
-
sourcestringThe AppleScript source code to compile and execute.
Return Value
An object `{ success, result, raw }`, or `null` on XPC failure.
hs.osascript.javascriptSync
Run an OSA JavaScript source string synchronously. Blocks the JS thread until the script completes.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:142
Declaration
hs.osascript.javascriptSync(source) -> [String: Any]
Parameters
-
sourcestringThe OSA JavaScript source code to compile and execute.
Return Value
An object `{ success, result, raw }`, or `null` on XPC failure.
hs.osascript.applescriptSyncFromFile
Read a file from disk and execute its contents as AppleScript synchronously.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:148
Declaration
hs.osascript.applescriptSyncFromFile(path) -> [String: Any]
Parameters
-
pathstringAbsolute path to the AppleScript source file.
Return Value
An object `{ success, result, raw }`, or `null` on XPC failure.
hs.osascript.javascriptSyncFromFile
Read a file from disk and execute its contents as OSA JavaScript synchronously.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:154
Declaration
hs.osascript.javascriptSyncFromFile(path) -> [String: Any]
Parameters
-
pathstringAbsolute path to the OSA JavaScript source file.
Return Value
An object `{ success, result, raw }`, or `null` on XPC failure.
hs.osascript._executeSync
Low-level synchronous execution entry point. Prefer `applescriptSync()` or `javascriptSync()` over calling this directly.
Hammerspoon 2/Modules/hs.osascript/HSOSAScriptModule.swift:164
Declaration
hs.osascript._executeSync(source, language) -> [String: Any]
Parameters
-
sourcestringThe script source code.
-
languagestringThe OSA language name — must be `"AppleScript"` or `"JavaScript"`.
Return Value
An object `{ success, result, raw }`, or `null` on XPC failure.