API Docs

Module 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

boolean

Whether Hammerspoon 2 is currently connected to the IPC broker, and so can receive commands from hs2.

Connecting happens in the background, so this becomes true shortly after start() is called.

Methods

hs.ipc.start() -> None

Start accepting commands from hs2.

Registers the IPC broker with launchd (if it isn't already registered) and connects to it. In release builds, only processes signed with the same Team ID can connect. Calling start() when already running logs a warning and does nothing. Problems, such as the broker having been disabled in System Settings, are logged to the console.

Only one running copy of Hammerspoon 2 can receive commands from hs2 at a time. If another copy calls start(), it takes over, and this copy stops (with a warning in the console).

hs.ipc.start() -> None
None
hs.ipc.start()

hs.ipc.stop() -> None

Stop accepting commands from hs2.

Disconnects from the IPC broker. Any running hs2 sessions stay open, and will work again if start() is called.

hs.ipc.stop() -> None
None
hs.ipc.stop()

hs.ipc.installBinary(directory) -> boolean

Install the hs2 command-line tool to the given directory as a symlink.

Creates a symlink in the target directory that points to the hs2 binary inside the Hammerspoon 2 app bundle. Using a symlink means the CLI automatically reflects any app update without reinstalling. Any existing hs2 file at that path is replaced.

The directory must be on your $PATH for hs2 to work without a full path.

Permissions: /usr/local/bin is typically user-writable on Intel Macs with Homebrew. On Apple Silicon, prefer /opt/homebrew/bin. On a stock Mac (no Homebrew), both directories require root — if this method returns false, run the logged command in a terminal with sudo.

hs.ipc.installBinary(directory) -> boolean
Name Type Description
directory JSValue Directory to install into. Defaults to /usr/local/bin.
boolean
true on success, false on error (details logged to the console).
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

Remove the hs2 command-line tool from the given directory.

hs.ipc.uninstallBinary(directory) -> boolean
Name Type Description
directory JSValue Directory to remove from. Defaults to /usr/local/bin.
boolean
true on success, false if not found or on error.
hs.ipc.uninstallBinary()
hs.ipc.uninstallBinary("/opt/homebrew/bin")

hs.ipc.isBinaryInstalled(directory) -> boolean

Check whether the hs2 command-line tool exists at the given directory.

hs.ipc.isBinaryInstalled(directory) -> boolean
Name Type Description
directory JSValue Directory to check. Defaults to /usr/local/bin.
boolean
true if an hs2 binary exists at that path.
if (hs.ipc.isBinaryInstalled()) {
    console.log("hs2 CLI is available")
}