hammerspoon2-docs
    Preparing search index...

    Function load

    • 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.

      Parameters

      • name: string

        The Spoon's name, matching its directory name under Spoons/. It's also the name it's stored under on hs.spoons.

      • Optionaloptions: { path?: string } | null

        Where to load the Spoon from

        • { path?: string }
          • Optionalpath?: 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.

        • null

      Returns Object

      Whatever the Spoon's init.js assigned to module.exports

      Throws an Error on failure; wrap calls in try/catch to handle it.