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 |
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));
Properties
This module has no properties.
Methods
hs.osascript.applescript(source) -> Promise<any>
hs.osascript.applescript(source) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| source | string | The AppleScript source code to compile and execute. |
hs.osascript.applescript('return "hello"').then(r => console.log(r.result))
hs.osascript.javascript(source) -> Promise<any>
hs.osascript.javascript(source) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| source | string | The OSA JavaScript source code to compile and execute. |
hs.osascript.javascript('Application("Finder").name()').then(r => console.log(r.result))
hs.osascript.applescriptFromFile(path) -> Promise<any>
hs.osascript.applescriptFromFile(path) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the AppleScript source file. |
hs.osascript.applescriptFromFile("/Users/me/scripts/notify.applescript")
.then(r => console.log(r.success))
hs.osascript.javascriptFromFile(path) -> Promise<any>
hs.osascript.javascriptFromFile(path) -> Promise<any>
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the OSA JavaScript source file. |
hs.osascript.javascriptFromFile("/Users/me/scripts/foo.jxa")
.then(r => console.log(r.result))
hs.osascript._execute(source, language) -> Promise<any>
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"`. |
hs.osascript._execute('return 1', "AppleScript").then(r => console.log(r.result))
hs.osascript.applescriptSync(source) -> [String: Any]
hs.osascript.applescriptSync(source) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| source | string | The AppleScript source code to compile and execute. |
const r = hs.osascript.applescriptSync('return "hello"')
console.log(r.result)
hs.osascript.javascriptSync(source) -> [String: Any]
hs.osascript.javascriptSync(source) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| source | string | The OSA JavaScript source code to compile and execute. |
const r = hs.osascript.javascriptSync('Application("Finder").name()')
console.log(r.result)
hs.osascript.applescriptSyncFromFile(path) -> [String: Any]
hs.osascript.applescriptSyncFromFile(path) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the AppleScript source file. |
const r = hs.osascript.applescriptSyncFromFile("/Users/me/foo.applescript")
hs.osascript.javascriptSyncFromFile(path) -> [String: Any]
hs.osascript.javascriptSyncFromFile(path) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| path | string | Absolute path to the OSA JavaScript source file. |
const r = hs.osascript.javascriptSyncFromFile("/Users/me/foo.jxa")
hs.osascript._executeSync(source, language) -> [String: Any]
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"`. |
const r = hs.osascript._executeSync('return 1', "AppleScript")