docs » hs.axuielement
This module allows you to access the accessibility objects of running applications, their windows, menus, and other user interface elements that support the OS X accessibility API.
This module works through the use of axuielementObjects, which is the Hammerspoon representation for an accessibility object. An accessibility object represents any object or component of an OS X application which can be manipulated through the OS X Accessibility API -- it can be an application, a window, a button, selected text, etc. As such, it can only support those features and objects within an application that the application developers make available through the Accessibility API.
In addition to the formal methods described in this documentation, dynamic methods exist for accessing element attributes and actions. These will differ somewhat between objects as the specific attributes and actions will depend upon the accessibility object's role and purpose, but the following outlines the basics.
Getting and Setting Attribute values:
object.attribute
is a shortcut forobject:attributeValue(attribute)
object.attribute = value
is a shortcut forobject:setAttributeValue(attribute, value)
- If detecting accessiblity errors that may occur is necessary, you must use the formal methods hs.axuielement:attributeValue and hs.axuielement:setAttributeValue
- Note that setting an attribute value is not guaranteeed to work with either method:
- internal logic within the receiving application may decline to accept the newly assigned value
- an accessibility error may occur
- the element may not be settable (surprisingly this does not return an error, even when hs.axuielement:isAttributeSettable returns false for the attribute specified)
- If you require confirmation of the change, you will need to check the value of the attribute with one of the methods described above after setting it.
Iteration over Attributes:
for k,v in pairs(object) do ... end
is a shortcut forfor k,_ in ipairs(object:attributeNames()) do local v = object:attributeValue(k) ; ... end
orfor k,v in pairs(object:allAttributeValues()) do ... end
(though see note below)- If detecting accessiblity errors that may occur is necessary, you must use one of the formal approaches hs.axuielement:allAttributeValues or hs.axuielement:attributeNames and hs.axuielement:attributeValue
- By default, hs.axuielement:allAttributeValues will not include key-value pairs for which the attribute (key) exists for the element but has no assigned value (nil) at the present time. This is because the value of
nil
prevents the key from being retained in the table returned. See hs.axuielement:allAttributeValues for details and a workaround.
Iteration over Child Elements (AXChildren):
for i,v in ipairs(object) do ... end
is a shortcut forfor i,v in pairs(object:attributeValue("AXChildren") or {}) do ... end
- Note that
object:attributeValue("AXChildren")
may return nil if the object does not have theAXChildren
attribute; the shortcut does not have this limitation.
- Note that
#object
is a shortcut for#object:attributeValue("AXChildren")
object[i]
is a shortcut forobject:attributeValue("AXChildren")[i]
- If detecting accessiblity errors that may occur is necessary, you must use the formal method hs.axuielement:attributeValue to get the "AXChildren" attribute.
Actions (hs.axuielement:actionNames):
object:do<action>()
is a shortcut forobject:performAction(action)
- See hs.axuielement:performAction for a description of the return values and hs.axuielement:actionNames to get a list of actions that the element supports.
ParameterizedAttributes:
object:<attribute>WithParameter(value)
is a shortcut for `object:parameterizedAttributeValue(attribute, value)-
See hs.axuielement:parameterizedAttributeValue for a description of the return values and hs.axuielement:parameterizedAttributeNames to get a list of parameterized values that the element supports
-
The specific value required for a each parameterized attribute is different and is often application specific thus requiring some experimentation. Notes regarding identified parameter types and thoughts on some still being investigated will be provided in the Hammerspoon Wiki, hopefully shortly after this module becomes part of a Hammerspoon release.
-
- Constants - Useful values which cannot be changed
- actions
- attributes
- orientations
- parameterizedAttributes
- roles
- rulerMarkers
- sortDirections
- subroles
- units
- Functions - API calls offered directly by the extension
- searchCriteriaFunction
- Constructors - API calls which return an object, typically one that offers API methods
- applicationElement
- applicationElementForPID
- systemElementAtPosition
- systemWideElement
- windowElement
- Methods - API calls which can only be made on an object returned by a constructor
- actionDescription
- actionNames
- allAttributeValues
- allDescendantElements
- asHSApplication
- asHSWindow
- attributeNames
- attributeValue
- attributeValueCount
- buildTree
- copy
- elementAtPosition
- elementSearch
- isAttributeSettable
- isValid
- matchesCriteria
- parameterizedAttributeNames
- parameterizedAttributeValue
- path
- performAction
- pid
- setAttributeValue
- setTimeout
Signature | hs.axuielement.actions[] |
---|---|
Type | Constant |
Description | A table of common accessibility object action names, provided for reference. |
Notes |
|
Signature | hs.axuielement.attributes[] |
---|---|
Type | Constant |
Description | A table of common accessibility object attribute names which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as keys in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.orientations[] |
---|---|
Type | Constant |
Description | A table of orientation types which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for "AXOrientation" in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.parameterizedAttributes[] |
---|---|
Type | Constant |
Description | A table of common accessibility object parameterized attribute names, provided for reference. |
Notes |
|
Signature | hs.axuielement.roles[] |
---|---|
Type | Constant |
Description | A table of common accessibility object roles which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for "AXRole" in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.rulerMarkers[] |
---|---|
Type | Constant |
Description | A table of ruler marker types which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for "AXMarkerType" in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.sortDirections[] |
---|---|
Type | Constant |
Description | A table of sort direction types which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for "AXSortDirection" in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.subroles[] |
---|---|
Type | Constant |
Description | A table of common accessibility object subroles which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for "AXSubrole" in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.units[] |
---|---|
Type | Constant |
Description | A table of measurement unit types which may be used with hs.axuielement:elementSearch or hs.axuielement:matchesCriteria as attribute values for attributes which specify measurement unit types (e.g. "AXUnits", "AXHorizontalUnits", and "AXVerticalUnits") in the match criteria argument. |
Notes |
|
Signature | hs.axuielement.searchCriteriaFunction(criteria) -> function |
---|---|
Type | Function |
Description | Returns a function for use with hs.axuielement:elementSearch that uses hs.axuielement:matchesCriteria with the specified criteria. |
Parameters |
|
Returns |
|
Signature | hs.axuielement.applicationElement(applicationObject) -> axuielementObject |
---|---|
Type | Constructor |
Description | Returns the top-level accessibility object for the application specified by the hs.application object. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.axuielement.applicationElementForPID(pid) -> axuielementObject |
---|---|
Type | Constructor |
Description | Returns the top-level accessibility object for the application with the specified process ID. |
Parameters |
|
Returns |
|
| Signature | hs.axuielement.systemElementAtPosition(x, y | pointTable) -> axuielementObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Constructor |
| Description | Returns the accessibility object at the specified position on the screen. The top-left corner of the primary screen is 0, 0. |
| Parameters |
x
- the x coordinate of the screen location to testy
- the y coordinate of the screen location to testpointTable
- the x and y coordinates of the screen location to test, provided as a point-table, like the one returned byhs.mouse.getAbsolutePosition
. A point-table is a table with key-value pairs for keysx
andy
.
- an axuielementObject for the object at the specified coordinates, or nil if no object could be identified.
- See also hs.axuielement:elementAtPosition -- this function is a shortcut for
hs.axuielement.systemWideElement():elementAtPosition(...)
.
Signature | hs.axuielement.systemWideElement() -> axuielementObject |
---|---|
Type | Constructor |
Description | Returns an accessibility object that provides access to system attributes. |
Parameters |
|
Returns |
|
Signature | hs.axuielement.windowElement(windowObject) -> axuielementObject |
---|---|
Type | Constructor |
Description | Returns the accessibility object for the window specified by the hs.window object. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.axuielement:actionDescription(action) -> string | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns a localized description of the specified accessibility object's action. |
| Parameters |
action
- the name of the action, as specified by hs.axuielement:actionNames.
- a string containing a description of the object's action, nil if no description is available, or nil and an error string if an accessibility error occurred
- The action descriptions are provided by the target application; as such their accuracy and usefulness rely on the target application's developers.
| Signature | hs.axuielement:actionNames() -> table | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns a list of all the actions the specified accessibility object can perform. |
| Parameters |
- None
- an array of the names of all actions supported by the axuielementObject or nil and an error string if an accessibility error occurred
- Common action names can be found in the hs.axuielement.actions table; however, this method will list only those names which are supported by this object, and is not limited to just those in the referenced table.
| Signature | hs.axuielement:allAttributeValues([includeErrors]) -> table | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns a table containing key-value pairs for all attributes of the accessibility object. |
| Parameters |
includeErrors
- an optional boolean, default false, that specifies whether attribute names which generate an error when retrieved are included in the returned results.
- a table with key-value pairs corresponding to the attributes of the accessibility object or nil and an error string if an accessibility error occurred
- if
includeErrors
is not specified or is false, then attributes which exist for the element, but currently have no value assigned, will not appear in the table. This is because Lua treats a nil value for a table's key-value pair as an instruction to remove the key from the table, if it currently exists. - To include attributes which exist but are currently unset, you need to specify
includeErrors
as true. - attributes for which no value is currently assigned will be given a table value with the following key-value pairs:
_code
= -25212error
= "Requested value does not exist"
Signature | hs.axuielement:allDescendantElements(callback, [withParents]) -> elementSearchObject |
---|---|
Type | Method |
Description | Query the accessibility object for all child accessibility objects and their descendants |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.axuielement:asHSApplication() -> hs.application object | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | If the element referes to an application, return an hs.application
object for the element. |
| Parameters |
- None
- if the element refers to an application, return an
hs.application
object for the element ; otherwise return nil
- An element is considered an application by this method if it has an AXRole of AXApplication and has a process identifier (pid).
| Signature | hs.axuielement:asHSWindow() -> hs.window object | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | If the element referes to a window, return an hs.window
object for the element. |
| Parameters |
- None
- if the element refers to a window, return an
hs.window
object for the element ; otherwise return nil
- An element is considered a window by this method if it has an AXRole of AXWindow.
| Signature | hs.axuielement:attributeNames() -> table | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns a list of all the attributes supported by the specified accessibility object. |
| Parameters |
- None
- an array of the names of all attributes supported by the axuielementObject or nil and an error string if an accessibility error occurred
- Common attribute names can be found in the hs.axuielement.attributes tables; however, this method will list only those names which are supported by this object, and is not limited to just those in the referenced table.
| Signature | hs.axuielement:attributeValue(attribute) -> value | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns the value of an accessibility object's attribute. |
| Parameters |
attribute
- the name of the attribute, as specified by hs.axuielement:attributeNames.
- the current value of the attribute, nil if the attribute has no value, or nil and an error string if an accessibility error occurred
| Signature | hs.axuielement:attributeValueCount(attribute) -> integer | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns the count of the array of an accessibility object's attribute value. |
| Parameters |
attribute
- the name of the attribute, as specified by hs.axuielement:attributeNames.
- the number of items in the value for the attribute, if it is an array, or nil and an error string if an accessibility error occurred
Signature | hs.axuielement:buildTree(callback, [depth], [withParents]) -> elementSearchObject |
---|---|
Type | Method |
Description | Captures all of the available information for the accessibility object and its descendants and returns it in a table for inspection. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.axuielement:copy() -> axuielementObject |
---|---|
Type | Method |
Description | Return a duplicate userdata reference to the Accessibility object. |
Parameters |
|
Returns |
|
| Signature | hs.axuielement:elementAtPosition(x, y | pointTable) -> axuielementObject | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns the accessibility object at the specified position on the screen. The top-left corner of the primary screen is 0, 0. |
| Parameters |
x
- the x coordinate of the screen location to tesy
- the y coordinate of the screen location to testpointTable
- the x and y coordinates of the screen location to test, provided as a point-table, like the one returned byhs.mouse.getAbsolutePosition
. A point-table is a table with key-value pairs for keysx
andy
.
- an axuielementObject for the object at the specified coordinates, or nil and an error string if no object could be identified or an accessibility error occurred
- This method can only be called on an axuielementObject that represents an application or the system-wide element (see hs.axuielement.systemWideElement).
Signature | hs.axuielement:elementSearch(callback, [criteria], [namedModifiers]) -> elementSearchObject |
---|---|
Type | Method |
Description | Search for and generate a table of the accessibility elements for the attributes and descendants of this object based on the specified criteria. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.axuielement:isAttributeSettable(attribute) -> boolean | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns whether the specified accessibility object's attribute can be modified. |
| Parameters |
attribute
- the name of the attribute, as specified by hs.axuielement:attributeNames.
- a boolean value indicating whether or not the value of the parameter can be modified or nil and an error string if an accessibility error occurred
| Signature | hs.axuielement:isValid() -> boolean | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns whether the specified accessibility object is still valid. |
| Parameters |
- None
- a boolean value indicating whether or not the accessibility object is still valid or nil and an error string if any other accessibility error occurred
- an accessibilityObject can become invalid for a variety of reasons, including but not limited to the element referred to no longer being available (e.g. an element referring to a window or one of its descendants that has been closed) or the application terminating.
Signature | hs.axuielement:matchesCriteria(criteria) -> boolean |
---|---|
Type | Method |
Description | Returns true if the axuielementObject matches the specified criteria or false if it does not. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.axuielement:parameterizedAttributeNames() -> table | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns a list of all the parameterized attributes supported by the specified accessibility object. |
| Parameters |
- None
- an array of the names of all parameterized attributes supported by the axuielementObject or nil and an error string if an accessibility error occurred
| Signature | hs.axuielement:parameterizedAttributeValue(attribute, parameter) -> value | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns the value of an accessibility object's parameterized attribute. |
| Parameters |
attribute
- the name of the attribute, as specified by hs.axuielement:parameterizedAttributeNames.parameter
- the parameter required by the paramaterized attribute.
- the current value of the parameterized attribute, nil if the parameterized attribute has no value, or nil and an error string if an accessibility error occurred
- The specific parameter required for a each parameterized attribute is different and is often application specific thus requiring some experimentation. Notes regarding identified parameter types and thoughts on some still being investigated will be provided in the Hammerspoon Wiki, hopefully shortly after this module becomes part of a Hammerspoon release.
Signature | hs.axuielement:path() -> table |
---|---|
Type | Method |
Description | Returns a table of axuielements tracing this object through its parent objects to the root for this element, most likely an application object or the system wide object. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.axuielement:performAction(action) -> axuielement | false | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Requests that the specified accessibility object perform the specified action. |
| Parameters |
action
- the name of the action, as specified by hs.axuielement:actionNames.
- if the requested action was accepted by the target, returns the axuielementObject; if the requested action was rejected, returns false; otherwise returns nil and an error string if an accessibility error occurred
- The return value only suggests success or failure, but is not a guarantee. The receiving application may have internal logic which prevents the action from occurring at this time for some reason, even though this method returns success (the axuielementObject). Contrawise, the requested action may trigger a requirement for a response from the user and thus appear to time out, causing this method to return false or nil.
| Signature | hs.axuielement:pid() -> integer | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns the process ID associated with the specified accessibility object. |
| Parameters |
- None
- the process ID for the application to which the accessibility object ultimately belongs or nil and an error string if an accessibility error occurred
| Signature | hs.axuielement:setAttributeValue(attribute, value) -> axuielementObject | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Sets the accessibility object's attribute to the specified value. |
| Parameters |
attribute
- the name of the attribute, as specified by hs.axuielement:attributeNames.value
- the value to assign to the attribute
- the axuielementObject on success; nil and an error string if the attribute could not be set or an accessibility error occurred.
| Signature | hs.axuielement:setTimeout(value) -> axuielementObject | nil, errString
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Sets the timeout value used accessibility queries performed from this element. |
| Parameters |
value
- the number of seconds for the new timeout value. Must be 0 or positive.
- the axuielementObject or nil and an error string if an accessibility error occurred
- To change the global timeout affecting all queries on elements which do not have a specific timeout set, use this method on the systemwide element (see hs.axuielement.systemWideElement.
- Changing the timeout value for an axuielement object only changes the value for that specific element -- other axuieleement objects that may refer to the identical accessibiity item are not affected.