API Docs

Properties

hs.onRelaunch

function | null
Called when Hammerspoon 2 is launched again while it's already running (e.g. from Finder, Spotlight or `open -a`), or when its Dock icon is clicked, or null to remove it. It's only called when Settings → Advanced → "When launched while running" is set to "Call hs.onRelaunch". If it's set to that and no callback is set (for example because your config failed to load), the Console is shown instead, so you can always get back to it.

hs.onOpenFile

function | null
Called when a file or folder is opened with Hammerspoon 2, e.g. by dropping it on the Dock icon, choosing Hammerspoon 2 from Finder's "Open With" menu, or `open -a "Hammerspoon 2" file.txt`, or null to remove it. It's called once for each item, with its path. Spoons (`.spoon2` files) are installed as usual rather than passed to it. Items opened before your config has loaded (for example, the one that launched Hammerspoon 2) are passed to it once your config has loaded.

hs.spoons

JSValue
A namespace holding every Spoon loaded so far via `loadSpoon()`, keyed by name - e.g. a Spoon loaded with `hs.loadSpoon("MySpoon")` is also reachable as `hs.spoons.MySpoon`. Empty until at least one Spoon has been loaded.

Methods

hs.reload() -> None

Destroy the current JavaScript runtime and start a new one, loading all configuration from disk again
hs.reload() -> None
None
hs.reload()

hs.collectGarbage() -> None

Force garbage collection of JavaScript objects that no longer have any references
hs.collectGarbage() -> None
None
This uses private macOS API
hs.collectGarbage()

hs.activeObjects() -> any[]

Get every object that is currently active: running tasks, scheduled timers, started watchers and so on. An active object keeps running, and its callbacks keep being called, until it finishes or you stop it, whether or not you kept a reference to it. Use this to find something you started and lost track of.
hs.activeObjects() -> any[]
any[]
The active objects
hs.activeObjects().forEach(obj => console.log(obj.toString()))

hs.openConsole() -> None

Open the Hammerspoon Console window
hs.openConsole() -> None
None
hs.openConsole()

hs.closeConsole() -> None

Close the Hammerspoon Console window
hs.closeConsole() -> None
None
hs.closeConsole()

hs.clearConsole() -> None

Clear the Hammerspoon Console log
hs.clearConsole() -> None
None
hs.clearConsole()

hs.loadSpoon(name) -> Object

Load a Spoon - a packaged, reusable piece of configuration - by name, from the `Spoons` directory inside your config directory. A Spoon must contain a well-formed `spoon.json` (with non-empty `name`, `author`, `version`, and `description` fields) and an `init.js`, or loading fails with an exception. `init.js` is loaded through the same `require()` used for the rest of your config, so it can itself `require()` further files from within the Spoon's own directory using relative paths. On success, the Spoon's `module.exports` is also stored on `hs.spoons` under its name, so other code can reach an already-loaded Spoon without needing to call `loadSpoon()` again. `init.js` must set `module.exports` to an object (or a function, since functions are objects too) - loading fails with an exception otherwise. Its `author`, `description`, and `version` properties are then set from `spoon.json`, overwriting any of the same name the Spoon's own `init.js` set, so that information is always present and always reflects what's on disk. If the resulting object has an `init()` method, it's called automatically (with `this` bound to the object) before `loadSpoon()` returns - matching Hammerspoon 1's behavior. An exception thrown from `init()` fails the load: nothing is stored on `hs.spoons`, and `loadSpoon()` throws. Unlike `init.js` itself (which `require()` only ever evaluates once), `init()` runs again on every `loadSpoon()` call for the same Spoon, since the same cached object is returned each time - write it to be safe to call more than once, or do one-time setup at `init.js`'s top level instead.
hs.loadSpoon(name) -> Object
Name Type Description
name string The Spoon's name, matching its directory name under `Spoons/`
Object
Whatever the Spoon's `init.js` assigned to `module.exports`
const MySpoon = hs.loadSpoon("MySpoon")
console.log(MySpoon.version)
// ...later, from anywhere...
hs.spoons.MySpoon.doSomething()