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.
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.consoleHistory(options) -> {sequence: number, date: Date, level: "garbage" | "debug" | "info" | "warning" | "error" | "javascript", message: string}[]
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.
Each message is an object with:
sequence: a number that's higher for each newer message. Pass the last one you've seen assinceto get only the messages logged after itdate: aDatefor when the message was loggedlevel:"garbage","debug","info","warning","error"or"javascript"(forconsole.log()and friends)message: the message's text
Options (all optional):
minimumLevel: 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 levelsearch: only get messages that contain this text, ignoring casesince: only get messages whosesequenceis higher than this
hs.consoleHistory(options) -> {sequence: number, date: Date, level: "garbage" | "debug" | "info" | "warning" | "error" | "javascript", message: string}[]
| Name | Type | Description |
|---|---|---|
| options | [String: Any] | Which messages to get |
// 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}`)
}
})