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
function | null
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
function | null
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.
Declaration
hs.urlevent.bind(eventName, callback) -> None
Parameters
| 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. |
Returns
None
Example
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.
Declaration
hs.urlevent.openURL(urlString) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| urlString | string | The URL to open. |
Returns
boolean
`true` if the URL was successfully dispatched.
Example
hs.urlevent.openURL("https://www.hammerspoon.org")
hs.urlevent.openURLWithBundle(urlString, bundleID) -> boolean
Open a URL with a specific application identified by bundle ID.
Declaration
hs.urlevent.openURLWithBundle(urlString, bundleID) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| urlString | string | The URL to open. |
| bundleID | string | Bundle identifier of the application to use. |
Returns
boolean
`true` if the URL was dispatched to the application.
Example
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.
Declaration
hs.urlevent.getDefaultHandler(scheme) -> string
Parameters
| Name | Type | Description |
|---|---|---|
| scheme | string | The scheme to query, without `://` (e.g. `"https"`, `"mailto"`). |
Returns
string
The bundle identifier string, or `null` if none is registered.
Example
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.
Declaration
hs.urlevent.getAllHandlersForScheme(scheme) -> string[]
Parameters
| Name | Type | Description |
|---|---|---|
| scheme | string | The scheme to query, without `://` (e.g. `"https"`, `"mailto"`). |
Returns
string[]
An array of bundle identifier strings.
Example
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.
Declaration
hs.urlevent.setDefaultHandler(scheme, bundleID) -> boolean
Parameters
| 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. |
Returns
boolean
`true` if the change was accepted by the system.
Example
hs.urlevent.setDefaultHandler("https", "com.apple.safari")