Search code examples
objective-cmacosaccessibilitymacos-carbon

macOS accessibility API on WebKit applications with AXTextMarker


I need to access data from webkit applications such as Safari, Mail and maybe others. I can see in the Accessibility Inspector there is :AXTextMarker and AXTextMarkerForRange.

I tried the usual way to get this info :

AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();    //creating system wide element
AXUIElementRef focussedElement = NULL;

AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement);                //Copy the focused
if (error != kAXErrorSuccess){
    NSLog(@"Could not get focused element");
}else{
    AXValueRef marker = NULL;
    AXError getTextValueError = AXUIElementCopyAttributeValue(focussedElement, kAXMarkerUIElementsAttribute , (CFTypeRef *)&marker);        
}

kAXMarkerUIElementsAttribute is the only thing I can see with Marker but everything is empty each time.

I guess for security reasons, I cannot access them? Is there any way possible. I am developing an app for people with difficulties reading and it could really help.

Thanks


Solution

  • Tips and tricks:

    Ask focussedElement for its supported attributes. Use the functions:

    AXError AXUIElementCopyAttributeNames(AXUIElementRef element, CFArrayRef  _Nullable *names);
    

    Returns a list of all the attributes supported by the specified accessibility object.

    and

    AXError AXUIElementCopyParameterizedAttributeNames(AXUIElementRef element, CFArrayRef  _Nullable *names);
    

    Returns a list of all the parameterized attributes supported by the specified accessibility object.

    Most undocumented attributes are self explanatory.

    For example get the selected text as attributed string:

    CFTypeRef markerRange = NULL;
    AXError error = AXUIElementCopyAttributeValue(focussedElement, (CFStringRef)@"AXSelectedTextMarkerRange", &markerRange);
    CFTypeRef result = NULL;
    error = AXUIElementCopyParameterizedAttributeValue(focussedElement, (CFStringRef)@"AXAttributedStringForTextMarkerRange", markerRange, &result);