Accessing text value from any System wide Application via Accessibility API
Suppose, I want to create a application which will monitor the typing and say if “macdevelopers” is typed anywhere be it in TextEdit or Mail etc. the application will perform a operation for example automatically opening the website in a browser.
The text value from any System wide Application can be accessed using the Accessibility API. In this article I will discuss about accessing the text value from any application like Text Edit or other applications if its current focused element is some text field or text view in it. Thus using this you can implement the functionality to access the active application’s text field value.
First of all, for using Accessibility API you must have enabled access for assistive devices in Universal Access preference pane in System Preferences.
Mark the check box “Enable Access for assistive devices” in Universal Access Preferences pane. (Note that in OS X 10.8 Universal Access pane has been renamed to Accessibility )
To programmatically enable access for assistive devices you may create a Apple Script file with the script below and execute this script from your application.
do shell script “touch /private/var/db/.AccessibilityAPIEnabled” with administrator privileges
Now, to access the text value from the current focused element of any application, follow the steps below:
AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); //creating system wide element
2. Retrieve the focussed element using kAXFocusedUIElementAttribute. Refer Apple’s documentation for other attributes.
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement); //Copy the focused element from the systemWideElement to focussedElement
if (error != kAXErrorSuccess) // if focused element could not be retrieved
NSLog(@”Could not get focused element”);
3. Now, if the focused element is retrieved, we need to access its string value.
AXValueRef textValue = NULL;
AXError gettextvalueError = AXUIElementCopyAttributeValue(focussedElement, kAXValueAttribute , (CFTypeRef *)&textValue); //get the text value of focussedElement in object textValue
NSString* stringVal = (NSString*)textValue; // Convert textValue to NSString
In this way you can use Accessibility API to accomplish various system wide tasks.
Posted on January 31, 2014, in Cocoa Application, Objective-C and tagged Accessibility Programming, Accessibiliy API, Assistive application, AXUIElement, Cocoa, Mac OS X, Mindfire Solutions, Neha Gupta, Objective-C, System Wide Element, X-Code. Bookmark the permalink. 4 Comments.