API Docs

Accessibility API Module

This module provides access to macOS's powerful Accessibility API, allowing you to:

  • Inspect UI elements in any application
  • Monitor window and element changes
  • Programmatically interact with UI elements

Basic Usage

// Get the focused UI element
const element = hs.ax.focusedElement();
console.log(element.role, element.title);

// Watch for window creation events
const app = hs.application.frontmost();
hs.ax.addWatcher(app, "AXWindowCreated", (notification, element) => {
    console.log("New window:", element.title);
});

Note: Requires accessibility permissions in System Preferences.

Types

This module provides the following types:

Properties

hs.ax.notificationTypes

{[key: string]: string}
A dictionary containing all of the notification types that can be used with hs.ax.addWatcher()

Methods

hs.ax.systemWideElement() -> HSAXElement

Get the system-wide accessibility element
hs.ax.systemWideElement() -> HSAXElement
HSAXElement
The system-wide AXElement, or nil if accessibility is not available
const sys = hs.ax.systemWideElement()

hs.ax.applicationElement(element) -> HSAXElement

Get the accessibility element for an application
hs.ax.applicationElement(element) -> HSAXElement
Name Type Description
element HSApplication An HSApplication object
HSAXElement
The AXElement for the application, or nil if accessibility is not available
const app = hs.application.frontmost()
const ax = hs.ax.applicationElement(app)

hs.ax.windowElement(window) -> HSAXElement

Get the accessibility element for a window
hs.ax.windowElement(window) -> HSAXElement
Name Type Description
window HSWindow An HSWindow object
HSAXElement
The AXElement for the window, or nil if accessibility is not available
const win = hs.window.focusedWindow()
const ax = hs.ax.windowElement(win)

hs.ax.elementAtPoint(point) -> HSAXElement

Get the accessibility element at the specific screen position
hs.ax.elementAtPoint(point) -> HSAXElement
Name Type Description
point HSPoint An HSPoint object containing screen coordinates
HSAXElement
The AXElement at that position, or nil if none found
const el = hs.ax.elementAtPoint({x: 100, y: 200})

hs.ax.addWatcher(application, notification, listener) -> None

Add a watcher for application AX events
hs.ax.addWatcher(application, notification, listener) -> None
Name Type Description
application HSApplication An HSApplication object
notification string An event name
listener function A function called with the notification name and the accessibility element it applies to
None
const app = hs.application.frontmost()
hs.ax.addWatcher(app, "AXWindowCreated", (notification, element) => {
    console.log("New window:", element.title)
})

hs.ax.removeWatcher(application, notification, listener) -> None

Remove a watcher for application AX events
hs.ax.removeWatcher(application, notification, listener) -> None
Name Type Description
application HSApplication An HSApplication object
notification string The event name to stop watching
listener function The function/lambda provided when adding the watcher
None
const app = hs.application.frontmost()
hs.ax.removeWatcher(app, "AXWindowCreated", myHandler)

hs.ax.focusedElement() -> HSAXElement

Fetch the focused UI element
hs.ax.focusedElement() -> HSAXElement
HSAXElement
An HSAXElement representing the focused UI element, or nil if none was found
const el = hs.ax.focusedElement()
console.log(el.role + " " + el.title)

hs.ax.findByRole(role, parent) -> HSAXElement[]

Find AX elements matching a given role
hs.ax.findByRole(role, parent) -> HSAXElement[]
Name Type Description
role string The role name to search for (e.g. "AXButton")
parent HSAXElement An HSAXElement to search within
HSAXElement[]
An array of matching HSAXElement objects
const app = hs.application.frontmost()
const buttons = hs.ax.findByRole("AXButton", hs.ax.applicationElement(app))

hs.ax.findByTitle(title, parent) -> HSAXElement[]

Find AX elements whose title contains a given string
hs.ax.findByTitle(title, parent) -> HSAXElement[]
Name Type Description
title string The string to search for within element titles
parent HSAXElement An HSAXElement to search within
HSAXElement[]
An array of matching HSAXElement objects
const app = hs.application.frontmost()
const matches = hs.ax.findByTitle("OK", hs.ax.applicationElement(app))

hs.ax.printHierarchy(element, maxDepth) -> None

Print the accessibility hierarchy of an element to the Console
hs.ax.printHierarchy(element, maxDepth) -> None
Name Type Description
element HSAXElement An HSAXElement to print. If omitted, the system-wide element is used
maxDepth number Maximum number of levels to traverse. Defaults to 5
None
const app = hs.application.frontmost()
hs.ax.printHierarchy(hs.ax.applicationElement(app), 3)