hs.shortcuts
ModuleRun and interact with macOS Shortcuts from JavaScript.
This module bridges to the Shortcuts app, letting you enumerate available shortcuts and run them from your Hammerspoon configuration.
Listing shortcuts
const all = hs.shortcuts.list()
all.forEach(s => console.log(s.name + " (accepts input: " + s.acceptsInput + ")"))
Running a shortcut
hs.shortcuts.run("My Shortcut").then(output => {
if (output) console.log("Output: " + output)
}).catch(err => console.log("Error: " + err))
Opening a shortcut for editing
hs.shortcuts.open("My Shortcut")
Properties
This module has no properties.
Methods
hs.shortcuts.list() -> [[String: Any]]
Returns an array of all available shortcuts.
| Key | Type | Description |
|-----|------|-------------|
| `name` | `string` | The display name of the shortcut |
| `id` | `string` | A UUID uniquely identifying the shortcut |
| `acceptsInput` | `boolean` | Whether the shortcut expects input when run |
| `actionCount` | `number` | How many actions the shortcut contains |
The shortcuts are read from Shortcuts Events, so Hammerspoon needs the Automation
permission for it; macOS asks the first time. An empty array means there are no
shortcuts: when they can't be read (the permission is denied, or nobody answers its
prompt within 2 minutes), `list()` throws instead.
Declaration
hs.shortcuts.list() -> [[String: Any]]
Returns
[[String: Any]]
An array of shortcut descriptor objects.
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
try {
hs.shortcuts.list().forEach(s => console.log(s.name))
} catch (err) {
console.error(err.message)
}
hs.shortcuts.run(name) -> Promise<string|null>
Runs a Shortcuts shortcut by name and returns any output.
Executes the shortcut in the background via the `shortcuts` CLI tool.
If the shortcut produces output (via a "Stop and Output" action), the
Promise resolves with that string. If the shortcut produces no output,
the Promise resolves with `null`. The Promise rejects if the shortcut
cannot be found or exits with a non-zero status.
Declaration
hs.shortcuts.run(name) -> Promise<string|null>
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | The exact display name of the shortcut to run. |
Returns
Promise<string|null>
A Promise resolving to the shortcut output string, or `null` if the shortcut produced no output.
Example
hs.shortcuts.run("My Shortcut")
.then(output => console.log("Done, output: " + output))
.catch(err => console.log("Failed: " + err))
hs.shortcuts.open(name) -> None
Opens a shortcut in the Shortcuts app for viewing or editing.
Uses the `shortcuts://open-shortcut` URL scheme to bring Shortcuts to
the foreground and navigate directly to the named shortcut.
Declaration
hs.shortcuts.open(name) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | The display name of the shortcut to open. |
Returns
None
Example
hs.shortcuts.open("My Shortcut")