API Docs

Module for storing small amounts of data that persists across Hammerspoon restarts.

Values are stored in a dedicated UserDefaults suite named "hs.userdefaults", kept separate from the app's own preferences so it doesn't get confused with Hammerspoon's own configuration when inspected with tools like the defaults command line utility.

JavaScript ↔ storage type mapping:

  • JavaScript strings, numbers, booleans, arrays and objects round-trip directly
  • JavaScript Date objects round-trip directly (no special API required)
  • JavaScript null/undefined are not storable values; use clear() to remove a key

Because the suite is just a standard macOS preferences domain, it can also be inspected and manipulated from the command line with the defaults tool:

# Read every value stored by hs.userdefaults
defaults read hs.userdefaults

# Read a single key
defaults read hs.userdefaults someKey

# Write a value from the command line
defaults write hs.userdefaults someKey "some value"

# Delete a key
defaults delete hs.userdefaults someKey
  • Note: This suite is not encrypted on disk. Do not use it to store sensitive information such as passwords, API keys, or tokens.

Properties

This module has no properties.

Methods

hs.userdefaults.set(key, value) -> None

Store a value under the given key. The value persists across Hammerspoon restarts. Values must be storable as a property list: strings, numbers, booleans, Dates, arrays, or objects (which may themselves nest any of those types). is rejected with a logged error and nothing is stored. JavaScript functions have no property-list representation; if passed directly, or nested inside an array or object, they are silently stored as an empty object.
hs.userdefaults.set(key, value) -> None
Name Type Description
key string The name of the setting
value any A string, number, boolean, Date, array, or object to store
None
Passing a native object from another `hs.*` module (e.g. a timer or hotkey)
hs.userdefaults.set("username", "chris")
hs.userdefaults.set("launchCount", 42)
hs.userdefaults.set("lastRun", new Date())

hs.userdefaults.get(key) -> any

Retrieve a previously stored value.
hs.userdefaults.get(key) -> any
Name Type Description
key string The name of the setting
any
The stored value, or null if nothing is stored under that key
const username = hs.userdefaults.get("username")

hs.userdefaults.clear(key) -> boolean

Delete a previously stored value.
hs.userdefaults.clear(key) -> boolean
Name Type Description
key string The name of the setting to remove
boolean
true if a value existed and was removed, false if the key was not set
hs.userdefaults.clear("username")

hs.userdefaults.getKeys() -> string[]

Get the names of all currently stored settings.
hs.userdefaults.getKeys() -> string[]
string[]
An array of setting names
console.log(hs.userdefaults.getKeys())

hs.userdefaults.addWatcher(key, listener) -> None

Watch a key for changes.
hs.userdefaults.addWatcher(key, listener) -> None
Name Type Description
key string The name of the setting to watch
listener function Called with the key and its new value whenever it changes
None
hs.userdefaults.addWatcher("username", (key, newValue) => {
    console.log(key + " changed to " + newValue)
})

hs.userdefaults.removeWatcher(key, listener) -> None

Remove a previously registered watcher.
hs.userdefaults.removeWatcher(key, listener) -> None
Name Type Description
key string The name of the setting originally passed to `addWatcher`
listener function The function originally passed to `addWatcher`
None
hs.userdefaults.removeWatcher("username", myHandler)