hs
ModuleProperties
hs.onRelaunch
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
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 rather than passed to it, unless
"Install Spoons when opened" is turned off in the Advanced tab of Settings.
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.onShutdown
Called just before your config is torn down,
or null to remove it. reason is "reload" when your config is being reloaded (including
by relaunching Hammerspoon 2, if Settings → Advanced → "Relaunch on reload" is on), or
"quit" when Hammerspoon 2 is quitting. Use it to undo anything that would outlast your
config, such as a system setting you changed, or to save state.
It's called before anything is shut down, so every module still works, and before the
shutdown() of any Spoons you've loaded (see the Spoons guide). It must finish its work
before it returns: your config is torn down straight afterwards, so timers, Promises and
callbacks it starts never complete. An exception it throws is logged, and doesn't stop
the teardown. Calling hs.reload() from it does nothing.
Processes started with hs.task are terminated straight after this callback and the
Spoons' shutdown() return. To run a command to completion, wait for it with
task.blockUntilExit(seconds), which gives up after the time you give it.
This replaces Hammerspoon 1's hs.shutdownCallback.
hs.accessibilityTimeout
How long, in seconds, an Accessibility request waits for an application to
answer before giving up, or 0 for the macOS default (about 6 seconds). This affects
everything that uses Accessibility, e.g. hs.window, hs.application, hs.ax and
hs.windowfilter.
A shorter timeout keeps Hammerspoon 2 responsive when an application is hung or slow, but a request to a busy application may then fail when it would have succeeded.
Code that asks many applications for things at once, such as hs.window.allWindows() and
hs.windowfilter, always waits at most 0.25 seconds, so one hung application can't stall
it. A shorter timeout set here applies to it too.
This goes back to 0 when your config is reloaded, so set it in your config.
Methods
hs.reload() -> None
Destroy the current JavaScript runtime and start a new one, loading all configuration from disk again
hs.reload() -> None
hs.reload()
hs.collectGarbage() -> None
Force garbage collection of JavaScript objects that no longer have any references
hs.collectGarbage() -> None
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[]
hs.activeObjects().forEach(obj => console.log(obj.toString()))
hs.openConsole() -> None
Open the Hammerspoon Console window
hs.openConsole() -> None
hs.openConsole()
hs.closeConsole() -> None
Close the Hammerspoon Console window
hs.closeConsole() -> None
hs.closeConsole()
hs.clearConsole() -> None
Clear the Hammerspoon Console log
hs.clearConsole() -> None
hs.clearConsole()
hs.checkForUpdates() -> boolean
Check for a newer version of Hammerspoon 2, the same as choosing "Check for Updates…" from its menu. If there is one, a window offers to install it. This is useful when Hammerspoon 2 is hidden from both the Dock and the menu bar, so that menu can't be reached.
hs.checkForUpdates() -> boolean
hs.checkForUpdates()
hs.consoleHistory(options) -> object[]
Get the messages currently in the Hammerspoon Console, oldest first. This is the same history
the Console window shows, so it's limited by the "Console history length" setting (which
applies to each level separately), and hs.clearConsole() empties it.
hs.consoleHistory(options) -> object[]
| Name | Type | Description |
|---|---|---|
| options optional | object | null | Which messages to get |
| minimumLevel optional | "garbage" | "debug" | "info" | "warning" | "error" | "javascript" | Only get messages at this level or above, e.g. "warning" for warnings and errors (and "javascript" messages, which count as the highest level). Defaults to every level |
| search optional | string | Only get messages that contain this text, ignoring case |
| since optional | number | Only get messages whose sequence is higher than this |
| Field | Type | Description |
|---|---|---|
| sequence | number | A number that's higher for each newer message. Pass the last one you've seen as since to get only the messages logged after it |
| date | Date | When the message was logged |
| level | "garbage" | "debug" | "info" | "warning" | "error" | "javascript" | The message's level, where "javascript" is for console.log() and friends |
| message | string | The message's text |
// Every 5 minutes, report how many errors were logged since the last check
let lastSeen = 0
const errorCheck = hs.timer.every(300, () => {
const errors = hs.consoleHistory({ minimumLevel: "error", since: lastSeen })
if (errors.length > 0) {
lastSeen = errors[errors.length - 1].sequence
// console.info(), not console.log(): "javascript" messages are above "error", so a
// console.log() report would be found as a new error by the next check
console.info(`${errors.length} new errors, the latest: ${errors[errors.length - 1].message}`)
}
})