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.addWatcher(listener) -> None
Create a watcher for application events
Declaration
hs.application.addWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | JSValue | A javascript function/lambda to call when any application event is received. The function will be called with two parameters: the name of the event, and the associated HSApplication object |
Returns
None
Example
hs.application.addWatcher((event, app) => {
console.log(event, app && app.title)
})
hs.application.removeWatcher(listener) -> None
Remove a watcher for application events
Declaration
hs.application.removeWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | JSValue | The javascript function/lambda that was previously being used to handle events |
Returns
None
Example
hs.application.removeWatcher(myHandler)