API Docs

Module 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
hs.application.runningApplications() -> HSApplication[]
HSApplication[]
An array of all currently running applications
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
hs.application.matchingName(name) -> HSApplication
Name Type Description
name string The applicaiton name to search for
HSApplication
The first matching application, or nil if none matched
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
HSApplication
The first matching application, or nil if none matched
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
HSApplication
The matching application, or nil if none matched
const app = hs.application.fromPID(1234)

hs.application.frontmost() -> HSApplication

Fetch the currently focused application
hs.application.frontmost() -> HSApplication
HSApplication
The matching application, or nil if none matched
const app = hs.application.frontmost()
console.log(app.title)

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")
string
The application's filesystem path, or nil if it was not found
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")
string[]
An array of strings containing any filesystem paths that were found
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
string
The path to an application for the given filetype, or il if none were found
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
string[]
An array of strings containing the filesystem paths for any applications that were found
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")
Promise<boolean>
A Promise that resolves to true if successful, false otherwise
hs.application.launchOrFocus("com.apple.Safari").then(ok => console.log(ok))

hs.application.addWatcher(listener) -> None

Create a watcher for application events
hs.application.addWatcher(listener) -> None
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
None
hs.application.addWatcher((event, app) => {
    console.log(event, app && app.title)
})

hs.application.removeWatcher(listener) -> None

Remove a watcher for application events
hs.application.removeWatcher(listener) -> None
Name Type Description
listener JSValue The javascript function/lambda that was previously being used to handle events
None
hs.application.removeWatcher(myHandler)