hs.application
ModuleModule for interacting with applications
Types
This module provides the following types:
Properties
This module has no properties.
Methods
hs.application.runningApplications() -> HSApplication[]
Fetch all running applications
Declaration
hs.application.runningApplications() -> HSApplication[]
Returns
HSApplication[]
An array of all currently running applications
Example
const apps = hs.application.runningApplications()
apps.forEach(a => console.log(a.title))
hs.application.matchingName(name) -> HSApplication
Fetch the first running application that matches a name
Declaration
hs.application.matchingName(name) -> HSApplication
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | The applicaiton name to search for |
Returns
HSApplication
The first matching application, or nil if none matched
Example
const safari = hs.application.matchingName("Safari")
hs.application.matchingBundleID(bundleID) -> HSApplication
Fetch the first running application that matches a Bundle ID
Declaration
hs.application.matchingBundleID(bundleID) -> HSApplication
Parameters
| Name | Type | Description |
|---|---|---|
| bundleID | string | The identifier to search for |
Returns
HSApplication
The first matching application, or nil if none matched
Example
const safari = hs.application.matchingBundleID("com.apple.Safari")
hs.application.fromPID(pid) -> HSApplication
Fetch the running application that matches a POSIX PID
Declaration
hs.application.fromPID(pid) -> HSApplication
Parameters
| Name | Type | Description |
|---|---|---|
| pid | number | The PID to search for |
Returns
HSApplication
The matching application, or nil if none matched
Example
const app = hs.application.fromPID(1234)
hs.application.frontmost() -> HSApplication
Fetch the currently focused application
Declaration
hs.application.frontmost() -> HSApplication
Returns
HSApplication
The matching application, or nil if none matched
Example
const app = hs.application.frontmost()
console.log(app.title)
hs.application.pathForBundleID(bundleID) -> string
Fetch the filesystem path for an application
Declaration
hs.application.pathForBundleID(bundleID) -> string
Parameters
| Name | Type | Description |
|---|---|---|
| bundleID | string | The application bundle identifier to search for (e.g. "com.apple.Safari") |
Returns
string
The application's filesystem path, or nil if it was not found
Example
const path = hs.application.pathForBundleID("com.apple.Safari")
hs.application.pathsForBundleID(bundleID) -> string[]
Fetch filesystem paths for an application
Declaration
hs.application.pathsForBundleID(bundleID) -> string[]
Parameters
| Name | Type | Description |
|---|---|---|
| bundleID | string | The application bundle identifier to search for (e.g. "com.apple.Safari") |
Returns
string[]
An array of strings containing any filesystem paths that were found
Example
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
Declaration
hs.application.pathForFileType(fileType) -> string
Parameters
| Name | Type | Description |
|---|---|---|
| fileType | string | The file type to search for. This can be a UTType identifier, a MIME type, or a filename extension |
Returns
string
The path to an application for the given filetype, or il if none were found
Example
const path = hs.application.pathForFileType("public.html")
hs.application.pathsForFileType(fileType) -> string[]
Fetch filesystem paths for applications able to open a given file type
Declaration
hs.application.pathsForFileType(fileType) -> string[]
Parameters
| Name | Type | Description |
|---|---|---|
| fileType | string | The file type to search for. This can be a UTType identifier, a MIME type, or a filename extension |
Returns
string[]
An array of strings containing the filesystem paths for any applications that were found
Example
const paths = hs.application.pathsForFileType("png")
hs.application.launchOrFocus(bundleID) -> Promise<boolean>
Launch an application, or give it focus if it's already running
Declaration
hs.application.launchOrFocus(bundleID) -> Promise<boolean>
Parameters
| Name | Type | Description |
|---|---|---|
| bundleID | string | A bundle identifier for the app to launch/focus (e.g. "com.apple.Safari") |
Returns
Promise<boolean>
A Promise that resolves to true if successful, false otherwise
Example
hs.application.launchOrFocus("com.apple.Safari").then(ok => console.log(ok))
hs.application.on(event, listener) -> None
Register a listener for application events.
Declaration
hs.application.on(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called when a matching application event occurs |
Returns
None
Example
hs.application.on('didLaunch', app => console.log("launched: " + app.title))
hs.application.on('didTerminate', app => console.log("terminated: " + (app && app.title)))
hs.application.off(event, listener) -> None
Remove a previously registered application event listener.
Declaration
hs.application.off(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event the listener was registered for |
| listener | any | The function originally passed to `on` |
Returns
None
Example
const onLaunch = app => console.log(app.title)
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.
Declaration
hs.application.once(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called the next time a matching event occurs, then automatically removed |
Returns
None
Example
hs.application.once('didLaunch', app => console.log("First launch seen: " + app.title))