API Docs

Object representing an Accessibility element. You should not instantiate this directly, but rather, use the hs.ax methods to create these as required.

Properties

role

string

The element's role (e.g., "AXWindow", "AXButton")

isEnabled

boolean

Whether the element is enabled

isFocused

boolean

Whether the element is focused

frame

The element's frame (position and size combined)

pid

number

Get the process ID of the application that owns this element

Methods

children() -> HSAXElement[]

The element's children

children() -> HSAXElement[]
An array of HSAXElement objects
const el = hs.ax.focusedElement()
console.log(el.children().length)

childAtIndex(index) -> HSAXElement

Get a specific child by index

childAtIndex(index) -> HSAXElement
Name Type Description
index number The index to fetch
An HSAXElement object, if a child exists at the given index
const el = hs.ax.focusedElement()
const child = el.childAtIndex(0)

attributeNames() -> string[]

Get all available attribute names

attributeNames() -> string[]
string[]
An array of attribute names
const el = hs.ax.focusedElement()
console.log(el.attributeNames())

attributeValue(attribute) -> any

Get the value of a specific attribute

Accessibility values are converted to JavaScript values: elements become HSAXElements, points HSPoints, sizes HSSizes, rectangles HSRects and ranges (e.g. AXSelectedTextRange) {location, length} objects (see HSAXRange). Strings, numbers, booleans, arrays and dictionaries are converted as you'd expect.

attributeValue(attribute) -> any
Name Type Description
attribute string The attribute name to fetch the value for
any
The requested value, or null if none was found
const el = hs.ax.focusedElement()
console.log(el.attributeValue("AXTitle"))

// The caret position (or selection) in a text field
const sel = el.attributeValue("AXSelectedTextRange")
console.log(`caret at ${sel.location}, ${sel.length} characters selected`)

setAttributeValue(attribute, value) -> boolean

Set the value of a specific attribute

The value must be of the type the Accessibility API expects for the attribute. For the well-known attributes below the type is checked first, and a value of the wrong type throws an error naming the expected type.

Attribute Expected value
AXPosition an HSPoint
AXSize an HSSize
AXFrame an HSRect
AXFocused, AXMain, AXMinimized, AXHidden, AXFrontmost, AXFullScreen, AXSelected, AXExpanded, AXDisclosing, AXEnhancedUserInterface a boolean
AXSelectedText a string
AXSelectedTextRange a range, e.g. {location: 0, length: 5} (see HSAXRange)
AXSelectedTextRanges a non-empty array of ranges

Plain objects such as {x: 1, y: 2} are not accepted for geometry attributes; use new HSPoint(1, 2). Ranges, however, are plain {location, length} objects, and any such object is sent to the Accessibility API as a range, whichever attribute it's for.

If the Accessibility API refuses the change, this throws an Error whose message says why, and whose code property is the Accessibility error's name, e.g. "kAXErrorAttributeUnsupported" (the element doesn't have the attribute), "kAXErrorIllegalArgument" (the value isn't valid for it) or "kAXErrorCannotComplete" (the application didn't respond). A value of the wrong type for one of the attributes above throws an Error with no code.

setAttributeValue(attribute, value) -> boolean
Name Type Description
attribute string The attribute name to set
value any The value to set
boolean
true if the attribute was set. Failures throw rather than returning false.
Throws an Error on failure; wrap calls in try/catch to handle it.
const el = hs.ax.focusedElement()
el.setAttributeValue("AXValue", "Hello")

const win = hs.ax.windowElement(hs.window.focusedWindow())
win.setAttributeValue("AXPosition", new HSPoint(100, 100))
win.setAttributeValue("AXSize", new HSSize(800, 600))

// Move the caret to the start of a text field
el.setAttributeValue("AXSelectedTextRange", {location: 0, length: 0})

try {
    win.setAttributeValue("AXFullScreen", true)
} catch (err) {
    console.error(err.message, err.code)
}

isAttributeSettable(attribute) -> boolean

Check if an attribute is settable

isAttributeSettable(attribute) -> boolean
Name Type Description
attribute string An attribute name
boolean
True if the attribute is settable, otherwise False
const el = hs.ax.focusedElement()
console.log(el.isAttributeSettable("AXValue"))

parameterizedAttributeNames() -> string[]

Get the names of the parameterized attributes the element supports

Parameterized attributes are attributes whose value depends on a parameter you give, e.g. the text in a given range of a text field (AXStringForRange). Get their values with parameterizedAttributeValue().

parameterizedAttributeNames() -> string[]
string[]
An array of parameterized attribute names
const el = hs.ax.focusedElement()
console.log(el.parameterizedAttributeNames())

parameterizedAttributeValue(attribute, parameter) -> any

Get the value of a parameterized attribute

Parameterized attributes answer a question about part of an element, mostly about its text. The parameter must be of the type the attribute expects; for the well-known attributes below it is checked first, and a parameter of the wrong type throws an error naming the expected type.

Attribute Parameter Value
AXStringForRange a range (see HSAXRange) the text in that range
AXAttributedStringForRange a range the text in that range, without its styling
AXBoundsForRange a range an HSRect: where that text is on screen
AXLineForIndex a character index the number of the line holding that character
AXRangeForLine a line number the range of that line's characters
AXRangeForIndex a character index the range of the composed character at that index
AXStyleRangeForIndex a character index the range of text styled like the character at that index
AXRangeForPosition an HSPoint on screen the range of the character at that point
AXCellForColumnAndRow an array [column, row] the HSAXElement for that table cell
AXLayoutPointForScreenPoint, AXScreenPointForLayoutPoint an HSPoint an HSPoint
AXLayoutSizeForScreenSize, AXScreenSizeForLayoutSize an HSSize an HSSize

Values are converted to JavaScript as for attributeValue(). Use parameterizedAttributeNames() to see which attributes an element supports. Web browsers mostly use their own text-marker attributes instead, which are not supported.

If the Accessibility API refuses the request, this throws an Error whose message says why, and whose code property is the Accessibility error's name, e.g. "kAXErrorIllegalArgument" (the parameter isn't valid, such as a range beyond the end of the text) or "kAXErrorCannotComplete" (the application didn't respond). An attribute the element doesn't support is not an error: it returns null. A parameter of the wrong type for one of the attributes above throws an Error with no code.

parameterizedAttributeValue(attribute, parameter) -> any
Name Type Description
attribute string The parameterized attribute name
parameter HSAXRange | number | number[] | HSPoint | HSSize | HSAXElement The parameter to pass
any
The attribute's value, or null if it has none, or the element doesn't support the attribute
Throws an Error on failure; wrap calls in try/catch to handle it.
const el = hs.ax.focusedElement()
const sel = el.attributeValue("AXSelectedTextRange")

// Where the caret is on screen, e.g. to show a window next to it
const caret = el.parameterizedAttributeValue("AXBoundsForRange", sel)
console.log(`caret is at ${caret.x}, ${caret.y}`)

// The text of the line the caret is on
const line = el.parameterizedAttributeValue("AXLineForIndex", sel.location)
const lineRange = el.parameterizedAttributeValue("AXRangeForLine", line)
console.log(el.parameterizedAttributeValue("AXStringForRange", lineRange))

actionNames() -> string[]

Get all available action names

actionNames() -> string[]
string[]
An array of available action names
const el = hs.ax.focusedElement()
console.log(el.actionNames())

performAction(action) -> boolean

Perform a specific action

If the Accessibility API refuses the action, this throws an Error whose message says why, and whose code property is the Accessibility error's name, e.g. "kAXErrorActionUnsupported" or "kAXErrorAttributeUnsupported" (the element doesn't have the action; AppKit applications report the latter) or "kAXErrorCannotComplete" (the application didn't respond). Use actionNames() to see which actions an element supports.

performAction(action) -> boolean
Name Type Description
action string The action to perform
boolean
true if the action was performed. Failures throw rather than returning false.
Throws an Error on failure; wrap calls in try/catch to handle it.
const el = hs.ax.focusedElement()
try {
    el.performAction("AXPress")
} catch (err) {
    console.error(err.message, err.code)
}

isEqualToElement(other) -> boolean

Test whether this element and another refer to the same underlying accessibility object

isEqualToElement(other) -> boolean
Name Type Description
other HSAXElement Another HSAXElement to compare against
boolean
True if both objects represent the same underlying accessibility element
const el1 = hs.ax.focusedElement()
const el2 = hs.ax.focusedElement()
console.log(el1.isEqualToElement(el2)) // true, assuming focus hasn't changed