HSApplication
TypeObject representing an application. You should not instantiate this directly in JavaScript, but rather, use the methods from hs.application which will return appropriate HSApplication objects.
Properties
allWindows
All windows of this application, including windows in native fullscreen and on other Spaces
(with the limitation described in hs.window.allWindows())
kind
The kind of application: "standard" (regular dock app), "accessory" (no dock), or "background" (agent)
Methods
kill() -> boolean
Terminate the application
kill() -> boolean
const app = hs.application.matchingName("Calculator")
app.kill()
kill9() -> boolean
Force-terminate the application
kill9() -> boolean
const app = hs.application.matchingName("Calculator")
app.kill9()
axElement() -> HSAXElement
The application's HSAXElement object, for use with the hs.ax APIs
axElement() -> HSAXElement
const app = hs.application.frontmost()
const ax = app.axElement()
activate(allWindows) -> boolean
Bring this application to the foreground
If the application has a focused window, that window is made the main window and only it is
brought forward (unless allWindows is true). This works even when Hammerspoon is not the
active application, e.g. from a hotkey callback.
activate(allWindows) -> boolean
| Name | Type | Description |
|---|---|---|
| allWindows | boolean | Pass true to raise all application windows. Defaults to false. |
const app = hs.application.matchingName("Safari")
app.activate()
app.activate(true)
setFrontmost(allWindows) -> boolean
Make this application the frontmost application
Unlike activate(), this does not change which of the application's windows is the main
window; whichever window is currently the application's main window is the one that comes forward.
setFrontmost(allWindows) -> boolean
| Name | Type | Description |
|---|---|---|
| allWindows | boolean | Pass true to raise all application windows, rather than just the main window. Defaults to false. |
const app = hs.application.matchingName("Safari")
if (!app.setFrontmost()) {
console.log("Unable to bring Safari to the front")
}
hide() -> None
Hide this application and all its windows
hide() -> None
const app = hs.application.matchingName("Safari")
app.hide()
unhide() -> None
Unhide this application
unhide() -> None
const app = hs.application.matchingName("Safari")
app.unhide()
getMenuItems() -> [[String: Any]]
Get the full menu structure of this application
getMenuItems() -> [[String: Any]]
const app = hs.application.frontmost()
app.getMenuItems().forEach(m => console.log(m.title))
findMenuItemByName(name) -> [String: Any]
Find a menu item by searching all menus for a matching title (case-insensitive)
findMenuItemByName(name) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| name | string | The menu item title to search for |
const app = hs.application.frontmost()
const item = app.findMenuItemByName("Select All")
console.log(item.enabled)
findMenuItemByPath(path) -> [String: Any]
Find a menu item by following a hierarchical path of titles
findMenuItemByPath(path) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| path | string[] | An array of menu titles forming a path from the top-level menu to the item, e.g. ["Edit", "Select All"] |
const app = hs.application.frontmost()
const item = app.findMenuItemByPath(["Edit", "Select All"])
console.log(item.enabled)
selectMenuItemByName(name) -> boolean
Click a menu item found by searching all menus for a matching title (case-insensitive)
selectMenuItemByName(name) -> boolean
| Name | Type | Description |
|---|---|---|
| name | string | The menu item title to search for |
const safari = hs.application.matchingName("Safari")
safari.selectMenuItemByName("New Window")
selectMenuItemByPath(path) -> boolean
Click a menu item found by following a hierarchical path of titles
selectMenuItemByPath(path) -> boolean
| Name | Type | Description |
|---|---|---|
| path | string[] | An array of menu titles forming a path from the top-level menu to the item, e.g. ["File", "New Window"] |
const safari = hs.application.matchingName("Safari")
safari.selectMenuItemByPath(["File", "New Window"])
findWindow(pattern) -> HSWindow[]
Find windows whose title contains the given string (case-insensitive)
findWindow(pattern) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| pattern | string | A string to search for in window titles |
const app = hs.application.frontmost()
app.findWindow("untitled").forEach(w => console.log(w.title))
getWindow(title) -> HSWindow
Get the first window with exactly the given title
getWindow(title) -> HSWindow
| Name | Type | Description |
|---|---|---|
| title | string | The exact window title to search for |
const app = hs.application.frontmost()
const win = app.getWindow("index.html")
toJSON() -> {[key: string]: any}
Returns a plain object describing this application, for JSON serialisation
JSON.stringify() calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
pid, bundleID, name, bundlePath, kind, isRunning, isActive and isHidden. Values that can't be read are nullconsole.log(JSON.stringify(hs.application.frontmost()))