API Docs

Spoons are packaged, reusable pieces of Hammerspoon configuration — a way to install someone else's automation (or share your own) without copy-pasting code into init.js. This guide covers installing and using a Spoon first, then how to write one, with a complete worked example. If you haven't read the Getting Started guide yet, especially its section on object lifecycle and splitting a config across multiple files, start there — everything below builds on it.

This part of Hammerspoon 2 is newer and smaller in scope than the rest of the app. One piece of Hammerspoon 1 lifecycle is built in - init() is called automatically, same as v1's :init()

  • but start()/stop() and bindHotkeys() below are recommended patterns for Spoon authors to follow, not things the runtime calls for you (unless you ask hs.spoons.use() to).

Finding Spoons#

There's no central list of Spoons: authors publish them in their own GitHub repositories, and you choose where each one comes from. Good places to look:

  • The hammerspoon2-spoon topic on GitHub lists repositories whose authors have tagged them as containing Hammerspoon 2 Spoons. Each repository's README should say which Spoons it holds and how to configure them.
  • r/Hammerspoon is where people share Spoons and configs, and where you can ask whether something that does what you need already exists.

Make sure it's a Hammerspoon 2 Spoon (a .spoon2 bundle written in JavaScript) and not a Hammerspoon 1 .spoon, which won't load. As with any code you run in your config, read a Spoon before installing it: it runs with all the permissions you've granted Hammerspoon 2.

Installing Spoons from GitHub or a URL#

Most Spoons are shared through their authors' own GitHub repositories (see Finding Spoons above). The easiest way to use one is hs.spoons.use(), which installs the Spoon the first time your config runs, and then loads and configures it:

hs.spoons.use("Greeter", {
    source: "jane/hs2-spoons",           // a GitHub repository
    ref: "v1.2.0",                       // optional: a tag, branch or commit
    config: { message: "Time for a break!" },
    hotkeys: { show: [["cmd", "alt"], "g"] },
    start: true
})

If the Spoon is already installed, use() loads and configures it straight away, without touching the network, so hs.spoons.Greeter works on the next line. If it isn't, it's downloaded and installed in the background, and configured once that finishes (with a notification to say so). use() returns a Promise that resolves with the Spoon once it's ready, so put anything that needs a freshly-installed Spoon in a .then(), or in the fn option, which is called with the Spoon after config and hotkeys are applied and before start():

hs.spoons.use("Greeter", {
    source: "jane/hs2-spoons",
    fn: (greeter) => greeter.show()
}).catch(err => console.error(`Couldn't use Greeter: ${err.message}`))

use()'s options#

hs.spoons.use(name, options) takes the Spoon's name (its directory name under Spoons/, and the name it's stored under on hs.spoons) and an object of options, all of them optional:

Option What it does
source Where to install the Spoon from if it isn't installed: a GitHub repository or a zip file's URL (see below). Without it, use() only uses an installed Spoon, and rejects with ENOTINSTALLED if there isn't one.
ref The tag, branch or commit to install from a GitHub source.
sha256 The SHA-256 hash a zip file source must have.
config An object whose properties are set on the Spoon.
hotkeys Passed to the Spoon's bindHotkeys() method. "default" passes the Spoon's own defaultHotkeys instead.
fn A function that's called with the Spoon, for any setup the other options don't cover.
start true calls the Spoon's start() method.
disable true makes use() do nothing at all, which is quicker than commenting the call out.

Once the Spoon is loaded (which runs its init(), if it has one), the options are applied in the order they're listed: config, then hotkeys, then fn, then start. So by the time the Spoon binds its hotkeys or starts, it's already been configured.

config is a shortcut for setting the Spoon's properties yourself. Each of its properties is copied onto the object the Spoon exports, replacing whatever was there, so these do the same thing:

hs.spoons.use("Greeter", { config: { message: "Time for a break!", interval: 1800 } })

const Greeter = hs.spoons.load("Greeter")
Greeter.message = "Time for a break!"
Greeter.interval = 1800

Which properties mean anything is up to the Spoon, so check its documentation. Nothing checks the names: a misspelled one just adds a property the Spoon never reads.

source can be:

  • a GitHub repository, as "owner/repo" or "https://github.com/owner/repo". ref picks a tag, branch or commit; without it, the repository's default branch is used.
  • the https:// URL of any zip file — a GitHub release download, a GitLab or Codeberg archive, or a file on your own web server. Pass sha256 with the file's SHA-256 hash, and the install fails rather than installing anything else.

Hammerspoon 2 finds the Spoons in what it downloads by looking for *.spoon2 directories at the top level, or in a Spoons directory. A repository or zip file that is a single Spoon (with spoon.json and init.js at its top level) works too.

The other functions give you finer control:

// What's in a repository?
hs.spoons.available("jane/hs2-spoons")
    .then(spoons => spoons.forEach(s => console.log(`${s.name} ${s.version}: ${s.description}`)))

// Install without loading
hs.spoons.install("Greeter", { source: "jane/hs2-spoons", ref: "v1.2.0" })
    .then(info => console.log(`Installed ${info.name} ${info.version}`))

// What's installed, and where from
hs.spoons.installed().forEach(s => console.log(`${s.name} ${s.version} from ${s.source}`))

// Download again from where it was installed from, and reinstall if its version changed
hs.spoons.update("Greeter").then(updated => console.log(updated ? "Updated" : "Up to date"))

hs.spoons.uninstall("Greeter")

Failures reject with an Error whose code says what went wrong (ENOTFOUND, ENETWORK, EINTEGRITY, EEXISTS, ...), so you can tell them apart in a .catch().

Only install Spoons from people you trust. A Spoon is code that runs with all of Hammerspoon 2's permissions. Pinning a version — a tag or commit with ref, or a sha256 for a zip file — means you keep running the code you chose, even if the repository changes later. use() never updates a Spoon that's already installed; update() is the only thing that does, and only when you call it. It fetches the Spoon from exactly where it was installed from, so a Spoon pinned to a tag or commit stays as it is — to move to a newer version, install it with the new ref and force: true.

Installing a Spoon by hand#

A Spoon is distributed as a .spoon2 bundle — a folder that macOS treats as a single double-clickable item, the same way it treats .app. Double-clicking one (or dragging it onto the Hammerspoon 2 icon) installs it: Hammerspoon 2 checks that it's well-formed, copies it into Spoons/ inside your config directory (typically ~/.config/Hammerspoon2/Spoons/), and confirms with a dialog once it's done. If a Spoon with the same name is already installed, you're asked to confirm before it's replaced — the dialog shows both the currently-installed and the new version so you can tell what's changing.

If you'd rather Hammerspoon 2 didn't install Spoons this way, turn off Install Spoons when opened in the Advanced tab of Settings. Opened .spoon2 bundles are then passed to your config like any other opened file (see hs.onOpenFile), instead of being installed.

Installing a Spoon doesn't load it. Loading is an explicit step in your init.js, covered next.

Using a Spoon#

Call hs.spoons.load("Name"), matching the Spoon's directory name under Spoons/:

const Greeter = hs.spoons.load("Greeter")

This throws if the Spoon is missing or malformed, so wrap it in a try/catch if you want your config to keep going without it rather than fail to load entirely:

try {
    const Greeter = hs.spoons.load("Greeter")
    Greeter.start()
} catch (e) {
    console.error(`Couldn't load Greeter: ${e.message}`)
}

load() also stores whatever it returns on hs.spoons, keyed by name, so anywhere else in your config can reach an already-loaded Spoon without holding onto the value yourself:

hs.spoons.load("Greeter")
// ...elsewhere...
hs.spoons.Greeter.show("Hello again")

If the Spoon exposes start()/stop() methods (see conventions below), call start() yourself after loading — nothing calls it for you:

const Greeter = hs.spoons.load("Greeter")
Greeter.start()

Anatomy of a Spoon#

A Spoon is a directory with two required files:

Greeter/
  spoon.json
  init.js

spoon.json carries the Spoon's identity. All four fields are required and must be non-empty, or hs.spoons.load() refuses to load it:

{
    "name": "Greeter",
    "author": "Jane Dev <jane@example.com>",
    "version": "1.0.0",
    "description": "Shows a friendly notification on a hotkey or a timer."
}

You never need to repeat this information inside init.js — after a successful load, hs.spoons.load() sets author, description, and version directly on the object your Spoon returns, reading them from spoon.json. This is a deliberate difference from Hammerspoon 1, where a Spoon's Lua table had to declare obj.name/obj.version/obj.author itself, alongside whatever else its init.lua set up — v2 gives you one source of truth instead of two that can drift apart.

init.js is loaded through the same require() used for the rest of your config (see Splitting a growing config into multiple files if you haven't read that section), so the same rules apply: it gets its own private scope, require('./lib/something.js') resolves relative to the Spoon's own directory, and __dirname points at that directory too — useful for loading bundled assets:

const iconPath = __dirname + "/icon.png"

init.js must set module.exports to an object (or a function — functions are objects in JS, so this works too) — hs.spoons.load() throws if it doesn't, since that's what the injected metadata needs to attach itself to. Keep the top level of init.js light: build the object and return it, but avoid starting timers, watchers, or hotkeys until start() is called (or, for a Spoon simple enough not to need a start()/stop() split at all, until the caller invokes whatever method actually needs them). Anything started keeps running until it's stopped (see object lifecycle), so stop() needs references to what start() started.

If the returned object has an init() method, it's called automatically — matching Hammerspoon 1, where :init() ran the same way. Use it for setup that needs to happen once the object exists but shouldn't wait for start() — reading a config file, computing a derived value, and so on. this inside init() refers to the object itself, and by the time it runs, author/description/version are already set on this, since metadata injection happens first. A thrown exception inside init() fails the whole load: hs.spoons.load() throws, and nothing is stored on hs.spoons.

One subtlety worth knowing: init.js's own top level only ever runs once (require() caches the module), but init() runs again every time hs.spoons.load() is called for that Spoon, since the same cached object is handed back each time. Write init() to be safe to call more than once — or better, do true one-time setup directly at init.js's top level, and reserve init() for things that are genuinely fine to redo.

Conventions worth following#

These conventions are worth following for consistency with how other Spoons will likely behave, and they're what hs.spoons.use()'s options rely on, even though — unlike init() — nothing in the runtime calls them for you when a Spoon is loaded:

Settings as properties of the exported object, with their defaults, so users can change them with use()'s config option or by setting them directly. config copies each of its properties onto your Spoon's object, and that's all it does. It doesn't call anything to tell you, so:

  • Read a setting from the object whenever you use it, rather than copying it into a variable. A variable in init.js can't be reached by config, and a copy taken earlier won't see the change.
  • Don't use settings in init(). It runs during loading, before config is applied, so it only ever sees the defaults. start(), bindHotkeys() and the methods users call all run after config, so they see the user's values.
  • Document each setting — its name, type and default — since that's how users find out what to put in config.
const greeter = module.exports = {
    message: "Hello from Greeter!",  // the notification's text
    interval: 3600,                  // seconds between notifications once started
    show, start, stop
}

function show() {
    hs.notify.show("Greeter", greeter.message)   // not a copy of it taken earlier
}

If a setting needs checking, or something has to happen when it changes, make it a property with a setter: config assigns properties, so the setter runs.

start() / stop() for a Spoon that does anything ongoing (timers, watchers, hotkeys bound persistently). Build the object and its configuration in init.js, but don't activate anything until start() runs:

function start() {
    if (timer) return          // already started
    timer = hs.timer.doEvery(3600, () => show())
}

function stop() {
    if (timer) { timer.stop(); timer = null }
}

bindHotkeys(mapping) for a Spoon whose behavior a user should be able to trigger with a hotkey, taking an object keyed by action name so the caller chooses their own keys rather than the Spoon hardcoding them:

function bindHotkeys(mapping) {
    if (mapping.show) {
        hs.hotkey.bind(mapping.show[0], mapping.show[1], () => show())
    }
}
// in the user's init.js
const Greeter = hs.spoons.load("Greeter")
Greeter.bindHotkeys({ show: [["cmd", "alt"], "g"] })

If your Spoon has sensible keys to suggest, export them as defaultHotkeys, in the same shape. Users can then pass hotkeys: "default" to hs.spoons.use() to bind them.

Logging: there's no per-Spoon logger like v1's hs.logger — use console.log()/.error() directly, optionally prefixing messages with the Spoon's name so they're identifiable in a config that's loaded several Spoons:

console.log("[Greeter] started")

A complete example#

A small Spoon that shows a notification on demand, optionally repeating on a timer, with a configurable message and interval, and a hotkey to trigger it immediately:

// Greeter/init.js
let timer = null

const greeter = module.exports = {
    // Settings, which users can change with hs.spoons.use()'s config option
    message: "Hello from Greeter!",   // the notification's text
    interval: 3600,                   // seconds between notifications once started

    defaultHotkeys: { show: [["cmd", "alt"], "g"] },

    show, init, start, stop, bindHotkeys
}

function show() {
    hs.notify.show("Greeter", greeter.message)
}

function init() {
    // Called automatically by hs.spoons.load(). author/description/version are already set on
    // `this` by this point - safe to log or read, but this could run more than once, so avoid
    // anything that shouldn't happen twice. Settings still have their defaults here: config is
    // applied after init() runs.
    console.log(`[Greeter] v${this.version} initialized`)
}

function start() {
    if (timer) return
    timer = hs.timer.doEvery(greeter.interval, show)
    console.log("[Greeter] started")
}

function stop() {
    if (timer) { timer.stop(); timer = null }
    console.log("[Greeter] stopped")
}

function bindHotkeys(mapping) {
    if (mapping.show) {
        hs.hotkey.bind(mapping.show[0], mapping.show[1], show)
    }
}
// Greeter/spoon.json
{
    "name": "Greeter",
    "author": "Jane Dev <jane@example.com>",
    "version": "1.0.0",
    "description": "Shows a friendly notification on a hotkey or a timer."
}

Using it:

// init.js
hs.spoons.use("Greeter", {
    config: { message: "Time for a break!", interval: 1800 },
    hotkeys: "default",
    start: true
})

or, doing the same thing by hand:

// init.js
const Greeter = hs.spoons.load("Greeter")
Greeter.message = "Time for a break!"
Greeter.interval = 1800
Greeter.bindHotkeys(Greeter.defaultHotkeys)
Greeter.start()

console.log(`Loaded ${Greeter.description} v${Greeter.version} by ${Greeter.author}`)

Sharing a Spoon#

Rename the Greeter folder to Greeter.spoon2 — that's the whole packaging step, since a .spoon2 bundle is the Spoon's directory, just with a different extension so macOS and Hammerspoon 2 recognize it as installable. Then either:

  • Push it to a GitHub repository. Put Greeter.spoon2 at the top of the repository (or in a Spoons directory, alongside any others you've written), and people can install it with hs.spoons.use("Greeter", { source: "you/your-repo" }). There's no build step and nothing to generate. Tag your releases, so people can pin a version with ref, and add the hammerspoon2-spoon topic to the repository, so it shows up in the list of Spoons on GitHub. Posting about it on r/Hammerspoon helps people find it too.
  • Hand someone the bundle, or a zip of it. Double-clicking it runs the install flow described above, and hs.spoons.install() can install the zip from any https:// URL.

Differences from Hammerspoon 1 Spoons, in brief#

If you're coming from v1, the shape is familiar but several specifics changed:

  • .spoon2, not .spoon — a deliberate rename so the two aren't confused; v1 and v2 Spoons aren't interchangeable, and existing v1 Spoons won't load in v2 unless rewritten in JavaScript.
  • spoon.json, not fields on the returned table. Required fields are name, author, version, description — v1 required name, author, version, license (with homepage optional). v2 doesn't currently check for a license field at all.
  • init() is called automatically, same as v1. One difference worth knowing: v2's require() caches init.js's top-level code (it only ever runs once), but init() itself runs again on every hs.spoons.load() call for that Spoon — see above for what that means for what you put in it.
  • hs.spoons has different helpers. v1's hs.spoons had resourcePath(), scriptPath(), and bindHotkeysToSpec(). v2's hs.spoons has load(), the installing functions above, and every loaded Spoon by name — use __dirname (built into require()) in place of resourcePath()/scriptPath(), and see bindHotkeys() above for the closest equivalent to bindHotkeysToSpec().
  • SpoonInstall is built in, as hs.spoons.use() (v1's andUse()) and friends, but there's no default repository: each Spoon names its own source.
  • JavaScript, not Lua — module.exports = {...} instead of return obj at the end of init.lua.

Where to go from here#

The Getting Started guide covers the rest of the API a Spoon is likely to use — hotkeys, timers, watchers, and hs.ui. Coming from Hammerspoon 1? The migration guide covers everything else that changed.