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
})
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 (or a function, since functions are
objects too) - loading fails with an exception otherwise. Its `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. 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.
Declaration
hs.spoons.load(name) -> Object
Parameters
Name
Type
Description
name
string
The Spoon's name, matching its directory name under `Spoons/`
Returns
Object
Whatever the Spoon's `init.js` assigned to `module.exports`
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
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).
`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.
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.
Declaration
hs.spoons.update(name) -> Promise<boolean>
Parameters
Name
Type
Description
name
string
The name of the installed Spoon
Returns
Promise<boolean>
Resolves with true if the Spoon was updated, or false if it was already up to date
Example
// 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}`))
}
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
call `hs.spoons.update()` for that.
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.
Declaration
hs.spoons.use(name, options) -> Promise
Parameters
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.
Returns
Promise
Resolves with the Spoon once it's loaded and configured, or null if `disable` is true