Search code examples
objective-cmacosaccessibilityappkitaccessibility-api

How to get the Objective-C class name corresponding to an AXUIElement?


Apple's Accessibility Inspector tool displays the Objective-C class corresponding to the currently inspected UI Element. See NSButtonCell at the very bottom of this screenshot:

Accessibility Inspector screenshot

However, how can I extract this information in my own code? The accessibility methods in Apple's seven year old UIElementInspector sample code do not surface the element's class name.

Reverse engineering the Accessibility Inspector hinted that the Objective-C class name could be obtained via the AXClassName attribute, but that attribute is always nil for me.


Solution

  • AppKit limits access to the AXClassName attribute, and several other attributes, to applications that have the com.apple.private.accessibility.inspection entitlement. The XPC service that Accessibility Inspector uses to access accessibility information has this entitlement. Since this is a private entitlement, I believe it can only be added to applications signed by an Apple certificate.

    You can confirm for yourself that the entitlement is key by stripping entitlements from the Accessibility Inspector's XPC service like so (make a back-up of Accessibility Inspector first!):

    codesign -f -s - /Applications/Xcode.app/Contents/Applications/Accessibility\ Inspector.app/Contents/Frameworks/AccessibilityAuditDeviceManager.framework/XPCServices/axAuditService.xpc
    

    If you then open Accessibility Inspector you'll see that most all of the functionality works correctly, except for things like the class name that are gated on the private entitlements.

    Accessibility Inspector with entitled XPC service

    Accessibility Inspector with unentitled XPC service