hs.userdefaults
ModuleModule 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
Dateobjects round-trip directly (no special API required) - JavaScript
null/undefinedare not storable values; useclear()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).
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 |
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 |
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 |
hs.userdefaults.clear("username")
hs.userdefaults.getKeys() -> string[]
Get the names of all currently stored settings.
hs.userdefaults.getKeys() -> string[]
console.log(hs.userdefaults.getKeys())
hs.userdefaults.on(key, listener) -> None
Watch a key for changes.
hs.userdefaults.on(key, listener) -> None
| Name | Type | Description |
|---|---|---|
| key | any | The name of the setting to watch |
| listener | any | Called with the new value whenever this key changes |
try {
hs.userdefaults.on("username", (newValue) => {
console.log("username changed to " + newValue)
})
} catch (err) {
console.error(err.message)
}
hs.userdefaults.off(key, listener) -> None
Remove a previously registered watcher.
hs.userdefaults.off(key, listener) -> None
| Name | Type | Description |
|---|---|---|
| key | any | The name of the setting originally passed to on |
| listener | any | The function originally passed to on |
hs.userdefaults.off("username", myHandler)
hs.userdefaults.once(key, listener) -> None
Register a listener that fires at most once for changes to a key.
hs.userdefaults.once(key, listener) -> None
| Name | Type | Description |
|---|---|---|
| key | any | The name of the setting to watch |
| listener | any | Called once, then automatically removed |
try {
hs.userdefaults.once("username", (newValue) => console.log("First change:", newValue))
} catch (err) {
console.error(err.message)
}