hs.ipc
ModuleModule for enabling CLI access to Hammerspoon 2 via the hs2 command-line tool.
IPC must be explicitly started from your configuration — it does not run by default.
Once started, the hs2 command-line tool can evaluate JavaScript interactively, with
optional live log streaming.
hs2 and Hammerspoon 2 talk to each other via XPC, through a small broker process
(HammerspoonIPCBroker) that ships inside the app bundle. The first call to start()
registers the broker with launchd as a background item, which macOS will tell you about
in a notification. If the broker is disabled in System Settings → General →
Login Items & Extensions, IPC will not work until it is re-enabled. launchd starts the
broker on demand, and it exits by itself when nothing has been connected for a while.
Communication is secured with a same-team code-signing requirement in release builds, so only binaries signed with the same Team ID can connect.
Quick start
// In your Hammerspoon 2 config (init.js):
hs.ipc.start()
Install the CLI tool once:
hs.ipc.installBinary() // symlinks hs2 to /usr/local/bin/hs2
Then in a terminal:
hs2
hs2> hs.reload()
undefined
hs2> 2 + 2
4
Run with live log output:
hs2 --log-level info
JavaScript can also be piped or redirected into hs2, one statement per line. When it
isn't running interactively, hs2 exits with status 65 if any of the lines threw an
error, and 1 if it couldn't reach Hammerspoon 2 at all:
echo 'hs.reload()' | hs2 --no-prompt || echo "failed with status $?"
hs2 --no-prompt < script.js
Properties
hs.ipc.isListening
Methods
hs.ipc.start() -> None
hs.ipc.start() -> None
hs.ipc.start()
hs.ipc.stop() -> None
hs.ipc.stop() -> None
hs.ipc.stop()
hs.ipc.installBinary(directory) -> boolean
hs.ipc.installBinary(directory) -> boolean
| Name | Type | Description |
|---|---|---|
| directory | JSValue | Directory to install into. Defaults to `/usr/local/bin`. |
hs.ipc.installBinary() // install to /usr/local/bin/hs2
hs.ipc.installBinary("/opt/homebrew/bin") // install to /opt/homebrew/bin/hs2
hs.ipc.uninstallBinary(directory) -> boolean
hs.ipc.uninstallBinary(directory) -> boolean
| Name | Type | Description |
|---|---|---|
| directory | JSValue | Directory to remove from. Defaults to `/usr/local/bin`. |
hs.ipc.uninstallBinary()
hs.ipc.uninstallBinary("/opt/homebrew/bin")
hs.ipc.isBinaryInstalled(directory) -> boolean
hs.ipc.isBinaryInstalled(directory) -> boolean
| Name | Type | Description |
|---|---|---|
| directory | JSValue | Directory to check. Defaults to `/usr/local/bin`. |
if (hs.ipc.isBinaryInstalled()) {
console.log("hs2 CLI is available")
}