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.on(event, listener) -> None

Register a listener for application events.
hs.application.on(event, listener) -> None
Name Type Description
event any The event to listen for
listener any Called when a matching application event occurs
None
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.
hs.application.off(event, listener) -> None
Name Type Description
event any The event the listener was registered for
listener any The function originally passed to `on`
None
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.
hs.application.once(event, listener) -> None
Name Type Description
event any The event to listen for
listener any Called the next time a matching event occurs, then automatically removed
None
hs.application.once('didLaunch', app => console.log("First launch seen: " + app.title))