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

This module has no properties.

Methods

hs.spoons.load(name) -> Object

Load an installed Spoon by name. 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.

The exception's code is ENOTINSTALLED if there's no such Spoon, EINVALIDARG if the name can't be a Spoon's, or EINVALIDSPOON if the Spoon is malformed. An exception thrown by the Spoon's own code is passed on as it is.

hs.spoons.load(name) -> Object
Name Type Description
name string The Spoon's name, matching its directory name under Spoons/
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)
}

hs.spoons.install(name, options) -> Promise<{name: string, version: string, author: string, description: string, source: string, ref: string | null, commit: string | null}>

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 [String: Any] source is a GitHub repository ("owner/repo" or its https://github.com/... URL) or the https:// URL of a zip file. ref is a branch, tag or commit to install from (GitHub only; the default branch if omitted). sha256 is the expected SHA-256 of the download. force: true replaces a Spoon that's already installed, rather than failing.
Promise<{name: string, version: string, author: string, description: string, source: string, ref: string | null, commit: string | null}>
Resolves with the installed Spoon's details
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<{name: string, version: string, author: string, description: string}[]>

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 [String: Any] As for install()
Promise<{name: string, version: string, author: string, description: string}[]>
Resolves with the Spoons found
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.

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
// Update every Spoon that was installed from a source
for (const spoon of hs.spoons.installed().filter(s => s.source)) {
    hs.spoons.update(spoon.name)
        .then(updated => { if (updated) console.log(`Updated ${spoon.name}`) })
        .catch(err => console.error(`${spoon.name}: ${err.message}`))
}

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() -> {name: string, version: string, author: string, description: string, source?: string, ref?: string | null, commit?: string | null}[]

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

hs.spoons.use(name, options) -> Promise

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.

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

        
        
Name Type Description
name any The name of the Spoon
options any source, ref and sha256 say where to install the Spoon from, as for hs.spoons.install(). config holds properties to set on the Spoon's object; which ones it reads is up to the Spoon. hotkeys is passed to the Spoon's bindHotkeys(), or "default" passes its defaultHotkeys. fn is called with the Spoon after it's configured. start: true calls its start(). disable: true does nothing at all, which is easier than commenting the call out.
Promise
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}`))