hs.application
ModuleModule for interacting with applications
Types
This module provides the following types:
Object Types
Plain JavaScript objects that this module's methods and properties take or return:
HSApplicationMenu
One of an application's menu bar menus
| Field | Type | Description |
|---|---|---|
| title | string | The menu's title, e.g. "File" |
| items optional | HSApplicationMenuItem[] | The items in the menu. Absent if the menu couldn't be read |
HSApplicationMenuItem
An item in one of an application's menus
| Field | Type | Description |
|---|---|---|
| title | string | The item's title, or "-" for a separator |
| enabled optional | boolean | Whether the item can be chosen. Absent for a separator |
| isSeparator optional | boolean | true if the item is a separator line |
| items optional | HSApplicationMenuItem[] | The items in the item's submenu, if it has one |
Event Types
The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:
HSApplicationEvent
An event name for hs.application.on(), hs.application.off() and hs.application.once()
| Event | Description |
|---|---|
"willLaunch" |
An application is about to launch |
"didLaunch" |
An application finished launching |
"didTerminate" |
An application quit |
"didHide" |
An application was hidden |
"didUnhide" |
An application was unhidden |
"didActivate" |
An application became the frontmost application |
"didDeactivate" |
An application stopped being the frontmost application |
"didChangeActivationPolicy" |
An application's activation policy (its kind) changed |
Properties
This module has no properties.
Methods
hs.application.runningApplications() -> HSApplication[]
Fetch all running applications
Hammerspoon 2's own helper processes (such as the one hs.osascript uses) are left out,
since they aren't applications you can interact with and don't answer Accessibility requests.
hs.application.runningApplications() -> HSApplication[]
const apps = hs.application.runningApplications()
apps.forEach(a => console.log(a.name))
hs.application.matchingName(name) -> HSApplication
Fetch the first running application that matches a name
Like runningApplications(), this never matches Hammerspoon 2's own helper processes.
hs.application.matchingName(name) -> HSApplication
| Name | Type | Description |
|---|---|---|
| name | string | The application name to search for |
const safari = hs.application.matchingName("Safari")
hs.application.matchingBundleID(bundleID) -> HSApplication
Fetch the first running application that matches a Bundle ID
hs.application.matchingBundleID(bundleID) -> HSApplication
| Name | Type | Description |
|---|---|---|
| bundleID | string | The identifier to search for |
const safari = hs.application.matchingBundleID("com.apple.Safari")
hs.application.fromPID(pid) -> HSApplication
Fetch the running application that matches a POSIX PID
hs.application.fromPID(pid) -> HSApplication
| Name | Type | Description |
|---|---|---|
| pid | number | The PID to search for |
const app = hs.application.fromPID(1234)
hs.application.frontmost() -> HSApplication
Fetch the currently focused application
hs.application.frontmost() -> HSApplication
const app = hs.application.frontmost()
console.log(app.name)
hs.application.pathForBundleID(bundleID) -> string
Fetch the filesystem path for an application
hs.application.pathForBundleID(bundleID) -> string
| Name | Type | Description |
|---|---|---|
| bundleID | string | The application bundle identifier to search for (e.g. "com.apple.Safari") |
const path = hs.application.pathForBundleID("com.apple.Safari")
hs.application.pathsForBundleID(bundleID) -> string[]
Fetch filesystem paths for an application
hs.application.pathsForBundleID(bundleID) -> string[]
| Name | Type | Description |
|---|---|---|
| bundleID | string | The application bundle identifier to search for (e.g. "com.apple.Safari") |
const paths = hs.application.pathsForBundleID("com.apple.Safari")
hs.application.pathForFileType(fileType) -> string
Fetch filesystem path for an application able to open a given file type
hs.application.pathForFileType(fileType) -> string
| Name | Type | Description |
|---|---|---|
| fileType | string | The file type to search for. This can be a UTType identifier, a MIME type, or a filename extension |
const path = hs.application.pathForFileType("public.html")
hs.application.pathsForFileType(fileType) -> string[]
Fetch filesystem paths for applications able to open a given file type
hs.application.pathsForFileType(fileType) -> string[]
| Name | Type | Description |
|---|---|---|
| fileType | string | The file type to search for. This can be a UTType identifier, a MIME type, or a filename extension |
const paths = hs.application.pathsForFileType("png")
hs.application.launchOrFocus(bundleID) -> Promise<boolean>
Launch an application, or give it focus if it's already running
hs.application.launchOrFocus(bundleID) -> Promise<boolean>
| Name | Type | Description |
|---|---|---|
| bundleID | string | A bundle identifier for the app to launch/focus (e.g. "com.apple.Safari") |
hs.application.launchOrFocus("com.apple.Safari").then(ok => console.log(ok))
hs.application.on(event, listener) -> None
Register a listener for application events.
Events are reported for every application that isn't background-only, including agents
(applications with no Dock icon, whose kind is "accessory"). A background-only application
is reported once it changes its activation policy (didChangeActivationPolicy), and like any
other from then on. Since it was already running, that change is not reported as a launch.
hs.application.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSApplicationEvent | The event to listen for |
| listener | (app: HSApplication | null) => void | Called when a matching application event occurs |
| Event | Description |
|---|---|
"willLaunch" |
An application is about to launch |
"didLaunch" |
An application finished launching |
"didTerminate" |
An application quit |
"didHide" |
An application was hidden |
"didUnhide" |
An application was unhidden |
"didActivate" |
An application became the frontmost application |
"didDeactivate" |
An application stopped being the frontmost application |
"didChangeActivationPolicy" |
An application's activation policy (its kind) changed |
try {
hs.application.on('didLaunch', app => console.log("launched: " + app.name))
hs.application.on('didTerminate', app => console.log("terminated: " + (app && app.name)))
} catch (err) {
console.error(err.message)
}
hs.application.off(event, listener) -> None
Remove a previously registered application event listener.
hs.application.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSApplicationEvent | The event the listener was registered for |
| listener | (app: HSApplication | null) => void | The function originally passed to on |
| Event | Description |
|---|---|
"willLaunch" |
An application is about to launch |
"didLaunch" |
An application finished launching |
"didTerminate" |
An application quit |
"didHide" |
An application was hidden |
"didUnhide" |
An application was unhidden |
"didActivate" |
An application became the frontmost application |
"didDeactivate" |
An application stopped being the frontmost application |
"didChangeActivationPolicy" |
An application's activation policy (its kind) changed |
const onLaunch = app => console.log(app.name)
hs.application.on('didLaunch', onLaunch)
// later…
hs.application.off('didLaunch', onLaunch)
hs.application.once(event, listener) -> None
Register a listener that fires at most once for an application event.
hs.application.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSApplicationEvent | The event to listen for |
| listener | (app: HSApplication | null) => void | Called the next time a matching event occurs, then automatically removed |
| Event | Description |
|---|---|
"willLaunch" |
An application is about to launch |
"didLaunch" |
An application finished launching |
"didTerminate" |
An application quit |
"didHide" |
An application was hidden |
"didUnhide" |
An application was unhidden |
"didActivate" |
An application became the frontmost application |
"didDeactivate" |
An application stopped being the frontmost application |
"didChangeActivationPolicy" |
An application's activation policy (its kind) changed |
try {
hs.application.once('didLaunch', app => console.log("First launch seen: " + app.name))
} catch (err) {
console.error(err.message)
}