could we use NSAccessibility API capture content from UI Element

I am trying to use NSAccessibility API to query the content UI element. For example the string from Safari URL Text Field


I have been experimenting AXUIElementRef API


I have a Test Application

1: monitoring NSWorkspaceDidLaunchApplicationNotification

>>

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];


/* Register for application launch notifications */

[[workspace notificationCenter] addObserver:self

selector:@selector(applicationLaunched:)

name:NSWorkspaceDidLaunchApplicationNotification

object:workspace];

>>


2:

In the applicationLaunched selector


2.1:

It checksNSApplicationBundleIdentifier = "com.apple.Safari";


2.2:

Then it uses

NSNumber *pidNumber =[[notification userInfo] valueForKey:@"NSApplicationProcessIdentifier"];

to get pid.


2.3:

Next, from the pid, it uses

AXUIElementRef element = AXUIElementCreateApplication(pid);

to get an AXUIElementRef


2.4:

When I trying to copy out the attribute from the AXUIElementRef element

i.e.

CFArrayRef arrayRefSys;

AXError e = AXUIElementCopyAttributeNames(element, &arrayRefSys);


I always get kAXErrorCannotComplete error.



P.S.:

And I validated from

NSDictionary *options = @{(__bridge id) kAXTrustedCheckOptionPrompt : @YES};

BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef) options);


I could confirm that accessibilityEnabled is YES in my test App.


I wonder if someone happen to familiar with this topic?


Thanks.