[launchd-dev] AXObserverAddNotification error on OS X 10.5 from an agent. What am I doing wrong?

eveningnick eveningnick eveningnick at gmail.com
Tue Nov 9 10:55:20 PST 2010


Hello
Why on OS X 10.5 i can't execute AXObserverAddNotification() from an
agent, while it works great being called from a process started using
a Terminal? (On OX X 10.6 it works great no matter how i am starting
my process - whether it's an agent or a "normal" app)? How could i
make it work on Mac OS Leopard as agent as well?

I have written a simple application that illustrates the problem i'd
run into (below), because no one answered to a "dry theoretical
question" i asked few times before :)

Here is a code, that reproduces the problem:
(the function "pid_t find_process(char *process_name)" returns PID of
a process by its name, a file "process.c", contains the implementation
of it, you can find it here: http://pastebin.com/b7sUbG4m )

------------------------------file: main.c------------------------------
#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

pid_t find_process(char *process_name);

void mycallback(AXObserverRef observer, AXUIElementRef element,
CFStringRef notification, void *refcon) {
	printf("Word Application has been activated!\n");
}

int main (int argc, const char * argv[]) {
	
    pid_t appPid = find_process("Microsoft Word");
	if(appPid==0) {
		printf("process doesn't exist");
		return 1;
	}
	
	AXObserverRef observer;
	AXError err;
	err = AXObserverCreate(appPid, mycallback, &observer);
    if(err!= kAXErrorSuccess) {
		printf("AXObserverCreate error: %i\n", err);
		return 1;
	}
	
	AXUIElementRef appElement = AXUIElementCreateApplication(appPid);
	if(appElement==NULL) {
		printf("AXUIElementCreateApplication error, returned 0");
	}
	
	err = AXObserverAddNotification(observer, appElement,
kAXApplicationActivatedNotification, NULL);
	if(err!= kAXErrorSuccess) {
		printf("AXObserverAddNotification error: %i\n", err);
		return 1;
	}
	
	CFRunLoopAddSource(CFRunLoopGetCurrent(),
AXObserverGetRunLoopSource(observer), kCFRunLoopCommonModes);
	
	CFRunLoopRun();	//run runloop indefinitely
    return 0;
}
------------------------------end------------------------------

if you compile that (having added frameworks CoreFoundation and
ApplicationServices  to the project) and launch it from a terminal,
everytime you activate Microsoft Word application, you will get a text
"Word Application has been activated" in a terminal window

now, if you create a textfile com.test.accessibility.agent.plist, with
the following content:

------------------------------com.test.accessibility.agent.plist------------------------------
<?xml version="1.0" encoding="UTF-8"?>
           <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd >
           <plist version="1.0">
           <dict>
                <key>Label</key>
                <string>com.test.accessibility.agent</string>
                <key>Program</key>
                <string>[HERE SPECIFY AN ABSOLUTE PATH TO THE
EXECUTABLE]</string>
                <key>KeepAlive</key>
                <true/>

           </dict>
           </plist>
---------------------------------------------end------------------------------

and then, execute the command from a terminal to install and launch the agent
% launchctl load com.test.accessibility.agent.plist
% launchctl start com.test.accessibility.agent

in system Console (/Applications/Utilities/Console.app) you will see
the following output

-----------------output in console---------------
com.test.accessibility.agent[1796] AXObserverAddNotification error: -25204
com.apple.launchd[84] (com.test.accessibility.agent[1799]) Exited with
exit code: 1
com.apple.launchd[84] (com.test.accessibility.agent) Throttling
respawn: Will start in 10 seconds
---------------end---------------

repeating every 10 secs, because of autorelaunch enabled for this agent in plist

(don't forget to "%launchctl remove com.test.accessibility.agent" to
stop it and delete its plist entry in the end)

What causes that? Can i somehow make it work on Mac OS 10.5 as an
agent? As i have mnentioned, on OS X 10.6 this is "fixed" (or, rather,
changed). Maybe my process, being started by launchd, doesn't have
sufficient rights to call this AXObserverAddNotification() function?
How can i increase these privileges, and to what level should i
increase them?
Unfortunately this document
http://developer.apple.com/library/mac/#documentation/Accessibility/Reference/AccessibilityLowlevel/AXUIElement_h
doesn't give any information

Thank you
George


More information about the launchd-dev mailing list