hs.urlevent
ModuleHandle URL events received by Hammerspoon 2.
The module responds to hammerspoon2:// URLs and, when Hammerspoon 2 is
configured as the system default handler, also to http://, https://,
and mailto: URLs.
Responding to custom hammerspoon2:// events
URLs take the form hammerspoon2://eventName?key=value&key2=value2.
The host component (eventName) selects the registered callback.
hs.urlevent.bind("myEvent", (eventName, params, pid, url) => {
console.log("param foo = " + params["foo"])
})
// Remove a binding
hs.urlevent.bind("myEvent", null)
Intercepting http / https / mailto URLs
Set hs.urlevent.httpCallback (or mailtoCallback) to a function.
You must also set Hammerspoon 2 as the system default handler for the
relevant scheme — see setDefaultHandler(_:_:).
hs.urlevent.httpCallback = (scheme, host, params, fullURL, pid) => {
// Forward to a real browser rather than swallowing the link
hs.urlevent.openURLWithBundle(fullURL, "com.apple.safari")
}
Querying and changing default handlers
const current = hs.urlevent.getDefaultHandler("https")
console.log("Current HTTPS handler: " + current)
const all = hs.urlevent.getAllHandlersForScheme("https")
console.log("Available: " + all.join(", "))
hs.urlevent.setDefaultHandler("https", "com.apple.safari")
Properties
hs.urlevent.httpCallback
Callback invoked when Hammerspoon 2 receives an http:// or https:// URL.
Fires only when Hammerspoon 2 is the system default handler for http/https.
Assign null to remove the callback.
hs.urlevent.mailtoCallback
Callback invoked when Hammerspoon 2 receives a mailto: URL.
Fires only when Hammerspoon 2 is the system default handler for mailto.
Assign null to remove the callback.
Methods
hs.urlevent.bind(eventName, callback) -> None
Register or remove a callback for a named hammerspoon2:// URL event.
The URL format is hammerspoon2://eventName?key=value. The host
component (eventName) selects the callback to invoke.
hs.urlevent.bind(eventName, callback) -> None
| Name | Type | Description |
|---|---|---|
| eventName | string | The URL host component identifying the event. |
| callback | function | A function receiving (eventName, params, senderPID, fullURL), or null to remove any existing binding. |
hs.urlevent.bind("myEvent", (name, params, pid, url) => {
console.log("myEvent — foo=" + params["foo"])
})
// Remove:
hs.urlevent.bind("myEvent", null)
hs.urlevent.openURL(urlString) -> boolean
Open a URL using the system default application for its scheme.
hs.urlevent.openURL(urlString) -> boolean
| Name | Type | Description |
|---|---|---|
| urlString | string | The URL to open. |
true if the URL was successfully dispatched.hs.urlevent.openURL("https://www.hammerspoon.org")
hs.urlevent.openURLWithBundle(urlString, bundleID) -> boolean
Open a URL with a specific application identified by bundle ID.
hs.urlevent.openURLWithBundle(urlString, bundleID) -> boolean
| Name | Type | Description |
|---|---|---|
| urlString | string | The URL to open. |
| bundleID | string | Bundle identifier of the application to use. |
true if the URL was dispatched to the application.hs.urlevent.openURLWithBundle("https://example.com", "com.apple.safari")
hs.urlevent.getDefaultHandler(scheme) -> string
Returns the bundle identifier of the default application for a URL scheme.
hs.urlevent.getDefaultHandler(scheme) -> string
| Name | Type | Description |
|---|---|---|
| scheme | string | The scheme to query, without :// (e.g. "https", "mailto"). |
null if none is registered.const handler = hs.urlevent.getDefaultHandler("https")
console.log("Default HTTPS handler: " + handler)
hs.urlevent.getAllHandlersForScheme(scheme) -> string[]
Returns all bundle identifiers capable of handling a URL scheme.
hs.urlevent.getAllHandlersForScheme(scheme) -> string[]
| Name | Type | Description |
|---|---|---|
| scheme | string | The scheme to query, without :// (e.g. "https", "mailto"). |
const browsers = hs.urlevent.getAllHandlersForScheme("https")
console.log("Available browsers: " + browsers.join(", "))
hs.urlevent.setDefaultHandler(scheme, bundleID) -> boolean
Set the default application for a URL scheme.
macOS may display a confirmation dialog for sensitive schemes such as
http and https. For custom schemes (hammerspoon2) no dialog is shown.
hs.urlevent.setDefaultHandler(scheme, bundleID) -> boolean
| Name | Type | Description |
|---|---|---|
| scheme | string | The scheme to configure, without :// (e.g. "https", "mailto"). |
| bundleID | string | Bundle identifier of the application to set as default. |
true if the change was accepted by the system.hs.urlevent.setDefaultHandler("https", "com.apple.safari")