API Docs

Module for receiving and posting macOS distributed notifications.

Distributed notifications are broadcast between processes through NSDistributedNotificationCenter. Many applications announce their changes this way: for example, Music posts com.apple.Music.playerInfo whenever its player changes state, with details such as Player State, Name and Artist in the notification's userInfo.

Notifications are delivered as soon as they're posted, even while Hammerspoon 2 is in the background.

  • Note: macOS only delivers notifications to observers that name the notification they want, so there is no way to listen for every distributed notification.

Properties

This module has no properties.

Methods

hs.distributednotifications.post(name, object, userInfo) -> boolean

Post a distributed notification, for other applications to receive.

The notification is delivered immediately, even to applications that are in the background.

hs.distributednotifications.post(name, object, userInfo) -> boolean
Name Type Description
name string The name of the notification, e.g. "com.example.myapp.somethingHappened"
object optional string An optional string identifying the sender, which observers receive as the notification's object
userInfo optional [String: Any] An optional object of extra information. Its values must be property list types: strings, numbers, booleans, Dates, arrays or objects of those types
boolean
true if the notification was posted, false if the name was empty or userInfo contained a value that can't be sent
hs.distributednotifications.post("com.example.hammerspoon.ping", "hammerspoon", { count: 1 })

hs.distributednotifications.on(name, listener) -> None

Listen for a distributed notification.

hs.distributednotifications.on(name, listener) -> None
Name Type Description
name string The name of the notification to listen for, e.g. "com.apple.Music.playerInfo"
listener (object: string | null, userInfo: Record<string, any>) => void Called with the notification's sender string (or null) and its userInfo object (empty if it has none) each time the notification is posted
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.distributednotifications.on("com.apple.Music.playerInfo", (object, userInfo) => {
        console.log(`Music is now ${userInfo["Player State"]}`)
    })
} catch (err) {
    console.error(err.message)
}

hs.distributednotifications.off(name, listener) -> None

Stop listening for a distributed notification.

hs.distributednotifications.off(name, listener) -> None
Name Type Description
name string The notification name originally passed to on
listener (object: string | null, userInfo: Record<string, any>) => void The function originally passed to on
None
hs.distributednotifications.off("com.apple.Music.playerInfo", myHandler)

hs.distributednotifications.once(name, listener) -> None

Listen for the next posting of a distributed notification only.

hs.distributednotifications.once(name, listener) -> None
Name Type Description
name string The name of the notification to listen for
listener (object: string | null, userInfo: Record<string, any>) => void Called once, then automatically removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.distributednotifications.once("com.apple.screenIsUnlocked", () => console.log("Screen unlocked"))
} catch (err) {
    console.error(err.message)
}