API Docs

Load, install and update Spoons - packaged, reusable pieces of configuration.

Spoons live in the Spoons directory inside your config directory. Every Spoon you load is also available on hs.spoons under its name, so hs.spoons.load("Greeter") makes hs.spoons.Greeter available to the rest of your config. That's why a Spoon can't be named after one of this module's own methods or properties.

Spoons can be installed from any GitHub repository ("owner/repo"), or from a zip file at any https:// URL. There's no central list of Spoons: you choose where each one comes from, so only install Spoons from sources you trust. Pin the version you install with ref (a GitHub tag or commit) or sha256 (for a zip URL), and nothing changes until you choose to.

Quick start

// Installs Greeter the first time this runs, then loads it, configures it and starts it
hs.spoons.use("Greeter", {
    source: "jane/hs2-spoons",
    ref: "v1.2.0",
    config: { message: "Time for a break!" },
    start: true
})

Properties

hs.spoons.isUpdating

boolean

Whether update() or updateAll() is running. It stays true for the whole of an updateAll().

Methods

hs.spoons.load(name, options) -> Object

Load an installed Spoon by name, or a Spoon from any directory with the path option. 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 is also stored on hs.spoons under its name, so other code can reach it without loading it again.

init.js must set module.exports to an object (not a function) - loading fails with an exception otherwise. Its name, 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 (a warning is logged if spoon.json's name doesn't match the Spoon's directory name). If the resulting object has an init() method, it's called automatically (with this bound to the object) before load() returns - matching Hammerspoon 1's behavior. An exception thrown from init() fails the load: nothing is stored on hs.spoons, and load() throws. Unlike init.js itself (which require() only ever evaluates once), init() runs again on every load() 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.

If the object has a bindings object (its actions, keyed by name), it's given a generic bindHotkeys(mapping) before init() runs, which binds the hotkeys in mapping (e.g. { show: [["cmd", "alt"], "g"] }) to those actions, and an unbindHotkeys(actions) that unbinds them again. These replace any bindHotkeys() and unbindHotkeys() the Spoon exports itself. A Spoon without bindings keeps its own, if it has them. See the Spoons guide for the details.

If the object has a shutdown() method, it's called (with this bound to the object) just before your config is torn down, with "reload" or "quit", as hs.onShutdown is. Loaded Spoons are shut down newest first, after hs.onShutdown.

path is for developing a Spoon: it loads the Spoon straight from that directory (such as a .spoon2 bundle in your own checkout of its repository), instead of from Spoons/, so reloading your config picks up your changes. Nothing is installed or copied, and a Spoon of the same name that's installed is ignored. A warning is logged each time, so a config that still points at a development copy doesn't go unnoticed.

A Spoon whose spoon.json has a minimumHSVersion isn't loaded by an older Hammerspoon 2 (see hs.appinfo.version): init.js isn't run, and load() throws, naming both versions.

The exception's code is ENOTINSTALLED if there's no such Spoon, ENOTFOUND if path isn't a directory, EINVALIDARG if the name can't be a Spoon's or an option is malformed, EINVALIDSPOON if the Spoon is malformed, or EVERSION if it needs a newer Hammerspoon 2. An exception thrown by the Spoon's own code is passed on as it is.

hs.spoons.load(name, options) -> Object
Name Type Description
name string The Spoon's name, matching its directory name under Spoons/. It's also the name it's stored under on hs.spoons.
options optional object | null Where to load the Spoon from
path optional string A directory to load the Spoon from instead of Spoons/. A relative path is relative to your config directory, and ~ is your home directory.
Object
Whatever the Spoon's init.js assigned to module.exports
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    const Greeter = hs.spoons.load("Greeter")
    console.log(Greeter.version)
    // ...later, from anywhere...
    hs.spoons.Greeter.show()
} catch (err) {
    console.error(err.message)
}

// While working on a Spoon, load it from your own copy of it
hs.spoons.load("MySpoon", { path: "~/src/my-spoons/MySpoon.spoon2" })

hs.spoons.install(name, options) -> Promise<object>

Download a Spoon and install it into the Spoons directory. It isn't loaded: call hs.spoons.load() (or use hs.spoons.use(), which does both) once it's installed.

The source is downloaded as a zip file. Its Spoons are every *.spoon2 directory at its top level or in a Spoons directory, or, if there are none, the whole archive, if it's a single Spoon. An archive that holds a single directory, as a repository's does, is looked inside.

The Promise rejects with an Error whose code says what went wrong: EINVALIDARG, ENOTFOUND (no such repository, ref, URL, or Spoon in the archive), ENETWORK, EINTEGRITY (the download didn't match sha256), EINVALIDSPOON, EEXISTS, or EIO (for anything else, such as failing to write the Spoon to disk).

hs.spoons.install(name, options) -> Promise<{name: string, version: string, author: string, description: string, source: string, ref: string | null, commit: string | null}>
Name Type Description
name string The name of the Spoon to install
options object | null Where to install the Spoon from
source string A GitHub repository ("owner/repo" or its https://github.com/... URL) or the https:// URL of a zip file
ref optional string A branch, tag or commit to install from (GitHub only; the default branch if omitted)
sha256 optional string The expected SHA-256 of the download
force optional boolean true replaces a Spoon that's already installed, rather than failing
Promise<object>
Resolves with the installed Spoon's details
Field Type Description
name string
version string
author string
description string
source string
ref string | null
commit string | null
hs.spoons.install("Greeter", { source: "jane/hs2-spoons", ref: "v1.2.0" })
    .then(info => console.log(`Installed ${info.name} ${info.version}`))
    .catch(err => console.error(`${err.code}: ${err.message}`))

hs.spoons.available(source, options) -> Promise<object[]>

List the Spoons that can be installed from a source, without installing anything.

hs.spoons.available(source, options) -> Promise<{name: string, version: string, author: string, description: string}[]>
Name Type Description
source string A GitHub repository ("owner/repo" or its https://github.com/... URL) or the https:// URL of a zip file
options optional object | null Where to look, as for install()
ref optional string A branch, tag or commit to look in (GitHub only; the default branch if omitted)
sha256 optional string The expected SHA-256 of the download
Promise<object[]>
Resolves with the Spoons found
Field Type Description
name string
version string
author string
description string
hs.spoons.available("jane/hs2-spoons").then(spoons => {
    spoons.forEach(s => console.log(`${s.name} ${s.version}: ${s.description}`))
})

hs.spoons.update(name) -> Promise<boolean>

Update an installed Spoon: download it again from where it was installed from (the same source, ref and URL), and reinstall it if its version has changed. A Spoon installed from a pinned tag, commit or versioned URL doesn't change; install the newer one with install(name, { ..., force: true }) instead.

Only the files on disk change. If the Spoon is already loaded, it keeps running its old version until your config is reloaded, which calls the old version's shutdown() (if it has one) and then loads the new one.

Besides the codes install() uses, the Promise rejects with ENOTINSTALLED, ENOSOURCE for a Spoon that wasn't installed by install() (or was since replaced some other way, such as by opening a .spoon2 file), or ECANCELED if the Spoon was reinstalled while the update was downloading.

hs.spoons.update(name) -> Promise<boolean>
Name Type Description
name string The name of the installed Spoon
Promise<boolean>
Resolves with true if the Spoon was updated, or false if it was already up to date
hs.spoons.update("Greeter")
    .then(updated => { if (updated) console.log("Updated Greeter") })
    .catch(err => console.error(`Greeter: ${err.message}`))

hs.spoons.updateAll() -> Promise<object[]>

Update every installed Spoon, as update() does, logging each one to the Console as it's done, and then a summary. Spoons installed from the same source share one download, and different sources are downloaded one at a time. Spoons installed some other way than by install() (such as by opening a .spoon2 file) are skipped, since there's nowhere to update them from. A Spoon that fails to update doesn't stop the others. isUpdating is true until they've all finished.

Typing hs.spoons.updateAll() into the Console is enough: the updates start straight away, and their progress is logged there. The returned Promise is for code that wants the results. It never rejects: a Spoon that couldn't be updated has a status of "failed", and an error with the code and message update() would have rejected with.

hs.spoons.updateAll() -> Promise<{name: string, status: "updated" | "upToDate" | "skipped" | "failed", error: {code: string, message: string} | null}[]>
Promise<object[]>
Resolves with what happened to each installed Spoon, sorted by name
Field Type Description
name string
status "updated" | "upToDate" | "skipped" | "failed"
error object | null
code string
message string
hs.spoons.updateAll().then(results => {
    const updated = results.filter(r => r.status === "updated").map(r => r.name)
    if (updated.length > 0) hs.notify.show("Spoons updated", updated.join(", "))
})

hs.spoons.uninstall(name) -> boolean

Remove an installed Spoon. If it's already loaded, it keeps running until your config is reloaded, so stop it first if it has a stop() method.

hs.spoons.uninstall(name) -> boolean
Name Type Description
name string The name of the installed Spoon
boolean
true if the Spoon was removed, or false if it wasn't installed
Throws an Error on failure; wrap calls in try/catch to handle it.
hs.spoons.uninstall("Greeter")

hs.spoons.installed() -> object[]

List the installed Spoons. Those installed by install() also say where they came from.

hs.spoons.installed() -> {name: string, version: string, author: string, description: string, source?: string, ref?: string | null, commit?: string | null}[]
object[]
The installed Spoons, sorted by name
Field Type Description
name string
version string
author string
description string
source optional string
ref optional string | null
commit optional string | null
hs.spoons.installed().forEach(s => console.log(`${s.name} ${s.version} from ${s.source ?? "a file"}`))

hs.spoons.use(name, options) -> Promise<Object | null>

Load and configure a Spoon in one step, installing it first if it isn't installed yet - the built-in equivalent of Hammerspoon 1's SpoonInstall:andUse().

If the Spoon is already installed, it's loaded and configured before use() returns, so hs.spoons.<name> can be used on the very next line, and nothing is downloaded. If it isn't installed and source is given, it's installed in the background (see hs.spoons.install()), then loaded and configured, and a notification says so; put anything that needs the Spoon in fn, or after the returned Promise resolves. An installed Spoon is never updated by use(): call hs.spoons.update() for that.

path loads the Spoon from a directory of your own instead, as hs.spoons.load() does, for developing a Spoon: nothing is installed, and an installed copy is ignored. It takes precedence over source, ref and sha256, which are ignored while it's given, so a config can keep its source for everyone else and add path while you work on the Spoon.

Configuration is applied after the Spoon is loaded (and its init() has run), in this order: each property of config is set on the Spoon's object, exactly as if you'd assigned it yourself, hotkeys is passed to its bindHotkeys() method, fn is called with the Spoon, and finally start() is called if start is true.

The Promise rejects with an Error with a code, as hs.spoons.install()'s does, plus ENOTINSTALLED if the Spoon isn't installed and no source was given, or the error thrown by loading or configuring the Spoon.

hs.spoons.use(name, options) -> Promise<Object | null>
Name Type Description
name string The name of the Spoon
options optional object How to install, load and configure the Spoon
source optional string Where to install the Spoon from if it isn't installed, as for hs.spoons.install()
ref optional string The branch, tag or commit to install from, as for hs.spoons.install()
sha256 optional string The expected SHA-256 of the download, as for hs.spoons.install()
path optional string A directory to load the Spoon from instead (relative to your config directory), as for hs.spoons.load(). Takes precedence over source, ref and sha256
config optional Record<string, any> Properties to set on the Spoon's object; which ones it reads is up to the Spoon
hotkeys optional Record<string, any> | "default" Passed to the Spoon's bindHotkeys(), or "default" passes its defaultHotkeys
fn optional (spoon: Object) => void Called with the Spoon after it's configured
start optional boolean true calls the Spoon's start() last
disable optional boolean true does nothing at all, which is easier than commenting the call out
Promise<Object | null>
Resolves with the Spoon once it's loaded and configured, or null if disable is true
hs.spoons.use("Greeter", {
    source: "jane/hs2-spoons",
    ref: "v1.2.0",
    config: { message: "Time for a break!" },
    hotkeys: { show: [["cmd", "alt"], "g"] },
    fn: (greeter) => console.log(`Using Greeter ${greeter.version}`),
    start: true
}).catch(err => console.error(`Couldn't use Greeter: ${err.message}`))

// While working on Greeter, use your own copy of it. source is ignored until path is removed.
hs.spoons.use("Greeter", {
    source: "jane/hs2-spoons",
    path: "~/src/hs2-spoons/Greeter.spoon2",
    start: true
})