hs.osascript
ModuleRun 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 |
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:
async function restartMusic() {
await hs.osascript.applescript('tell application "Music" to pause');
await hs.osascript.applescript('tell application "Music" to set sound volume to 50');
await hs.osascript.applescript('tell application "Music" to play');
}
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.
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));
Properties
This module has no properties.
Methods
hs.osascript.applescript(source) -> Promise<any>
Run an AppleScript source string.
Scripts run in parallel; chain Promises if they must run in order (see the module overview).
hs.osascript.applescript(source) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| source | string | The AppleScript source code to compile and execute. |
Promise resolving to { success, result, raw }.hs.osascript.applescript('return "hello"').then(r => console.log(r.result))
hs.osascript.javascript(source) -> Promise<any>
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.
Scripts run in parallel; chain Promises if they must run in order (see the module overview).
hs.osascript.javascript(source) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| source | string | The OSA JavaScript source code to compile and execute. |
Promise resolving to { success, result, raw }.hs.osascript.javascript('Application("Finder").name()').then(r => console.log(r.result))
hs.osascript.applescriptFromFile(path) -> Promise<any>
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>" }.
hs.osascript.applescriptFromFile(path) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the AppleScript source file. |
Promise resolving to { success, result, raw }.hs.osascript.applescriptFromFile("/Users/me/scripts/notify.applescript")
.then(r => console.log(r.success))
hs.osascript.javascriptFromFile(path) -> Promise<any>
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>" }.
hs.osascript.javascriptFromFile(path) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the OSA JavaScript source file. |
Promise resolving to { success, result, raw }.hs.osascript.javascriptFromFile("/Users/me/scripts/foo.jxa")
.then(r => console.log(r.result))
hs.osascript._execute(source, language) -> Promise<any>
Low-level execution entry point used by the higher-level helpers.
Prefer applescript() or javascript() over calling this directly.
hs.osascript._execute(source, language) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| source | string | The script source code. |
| language | string | The OSA language name — must be "AppleScript" or "JavaScript". |
Promise resolving to { success, result, raw }.hs.osascript._execute('return 1', "AppleScript").then(r => console.log(r.result))
hs.osascript.applescriptSync(source) -> [String: Any]
Run an AppleScript source string synchronously.
Warning: blocks Hammerspoon's main thread until the script completes, so nothing else in Hammerspoon can run in the meantime. Prefer the async API.
hs.osascript.applescriptSync(source) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| source | string | The AppleScript source code to compile and execute. |
{ success, result, raw }, or null on XPC failure.const r = hs.osascript.applescriptSync('return "hello"')
console.log(r.result)
hs.osascript.javascriptSync(source) -> [String: Any]
Run an OSA JavaScript source string synchronously.
Warning: blocks Hammerspoon's main thread until the script completes, so nothing else in Hammerspoon can run in the meantime. Prefer the async API.
hs.osascript.javascriptSync(source) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| source | string | The OSA JavaScript source code to compile and execute. |
{ success, result, raw }, or null on XPC failure.const r = hs.osascript.javascriptSync('Application("Finder").name()')
console.log(r.result)
hs.osascript.applescriptSyncFromFile(path) -> [String: Any]
Read a file from disk and execute its contents as AppleScript synchronously.
Warning: blocks Hammerspoon's main thread until the script completes, so nothing else in Hammerspoon can run in the meantime. Prefer the async API.
hs.osascript.applescriptSyncFromFile(path) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the AppleScript source file. |
{ success, result, raw }, or null on XPC failure.const r = hs.osascript.applescriptSyncFromFile("/Users/me/foo.applescript")
hs.osascript.javascriptSyncFromFile(path) -> [String: Any]
Read a file from disk and execute its contents as OSA JavaScript synchronously.
Warning: blocks Hammerspoon's main thread until the script completes, so nothing else in Hammerspoon can run in the meantime. Prefer the async API.
hs.osascript.javascriptSyncFromFile(path) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the OSA JavaScript source file. |
{ success, result, raw }, or null on XPC failure.const r = hs.osascript.javascriptSyncFromFile("/Users/me/foo.jxa")
hs.osascript._executeSync(source, language) -> [String: Any]
Low-level synchronous execution entry point.
Prefer applescriptSync() or javascriptSync() over calling this directly.
Warning: blocks Hammerspoon's main thread until the script completes, so nothing else in Hammerspoon can run in the meantime. Prefer the async API.
hs.osascript._executeSync(source, language) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| source | string | The script source code. |
| language | string | The OSA language name — must be "AppleScript" or "JavaScript". |
{ success, result, raw }, or null on XPC failure.const r = hs.osascript._executeSync('return 1', "AppleScript")