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 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
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.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 as since to get only the messages logged after it
  • date: a Date for when the message was logged
  • level: "garbage", "debug", "info", "warning", "error" or "javascript" (for console.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 level
  • search: only get messages that contain this text, ignoring case
  • since: only get messages whose sequence is 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
{sequence: number, date: Date, level: "garbage" | "debug" | "info" | "warning" | "error" | "javascript", message: string}[]
The matching messages, oldest first
Throws an Error on failure; wrap calls in try/catch to handle it.
Messages reach the Console asynchronously, so one logged moments ago, including
// 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}`)
    }
})