I am thinking about using the Accessibility API to automate some UI tasks, but I am finding that writing the code to descend the accessibility object hierarchy requires a lot of code and is prone to accidental memory leaks if I forget to call CFRelease(). For example, here is code that I wrote to look for an AXSplitGroup
child of an accessibility object:
//AXError axErr;
//CFTypeRef typeRef;
AXUIElementRef axSplitGroup = NULL;
{
CFIndex objectChildrenCount;
axErr = AXUIElementGetAttributeValueCount(axObject, kAXChildrenAttribute, &objectChildrenCount);
assert(kAXErrorSuccess == axErr);
CFArrayRef objectChildren;
axErr = AXUIElementCopyAttributeValues(axObject, kAXChildrenAttribute, 0, objectChildrenCount, &objectChildren);
assert(kAXErrorSuccess == axErr);
assert(CFArrayGetCount(objectChildren) == objectChildrenCount);
for (CFIndex i = 0; i < objectChildrenCount; ++i) {
AXUIElementRef objectChild = CFArrayGetValueAtIndex(objectChildren, i);
axErr = AXUIElementCopyAttributeValue(objectChild, kAXRoleAttribute, &typeRef);
assert(kAXErrorSuccess == axErr);
assert([(__bridge id)typeRef isKindOfClass:[NSString class]]);
CFStringRef role = (CFStringRef)typeRef;
if (kCFCompareEqualTo == CFStringCompare(role, kAXSplitGroupRole, 0)) {
axSplitGroup = objectChild;
i = objectChildrenCount - 1;
}
CFRelease(role);
}
CFRelease(objectChildren);
assert(axSplitGroup);
}
Originally I was wondering (read: hoping) that Apple provided an API that would allow XPath-like expressions to be evaluated on the accessibility object hierarchy to retrieve accessibility object(s) of interest, but from some searching, I don't think that this exists.
Is there another standard API that can be used to iterate the accessibility object hierarchy without so much code?
Your best option is to adopt Apple's UI Automation, which allows you to query and interact with elements in the accessibility hierarchy via Instruments and a JavaScript API. This is the preferred technique for interface automation. You will directly benefit from any work you've already done to adopt UIAccessibility
.