Re: Eaves dropping on application events
Re: Eaves dropping on application events
- Subject: Re: Eaves dropping on application events
- From: Joshua Kifer <email@hidden>
- Date: Sun, 7 Dec 2003 17:37:28 -0500
Alright, I finally got a breakthrough with this thanks to some archived
messages from Bill Cheeseman and Guy English, as well as you others
that have been discussing this today.
For the code here, make sure to declare that observer function in the
header. I default to the first app in the list (the finder) for this
example. I used the constant "kAXApplicationActivatedNotification",
but there are others. When I first ran this, I got a value for
AXError that indicated the API was disabled. After enabling it in my
Universal Access preferences, the application succeeded in logging the
notification.
Here's the code:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSNotificationCenter* cleric = [workspace notificationCenter];
NSArray* apps = [workspace launchedApplications];
NSMutableDictionary* app;
app = [NSMutableDictionary dictionaryWithDictionary:[apps
objectAtIndex:0]];
pid_t pid = [[app valueForKey:@"NSApplicationProcessIdentifier"]
intValue];
AXObserverRef observer;
AXUIElementRef element;
AXError err;
err = AXObserverCreate(pid, observerCallbackFunction, &observer);
NSLog(@"Error: %d", err);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
AXObserverGetRunLoopSource(observer),
kCFRunLoopDefaultMode);
element = AXUIElementCreateApplication(pid);
[app setObject: element forKey: @"app_element"];
[app setObject: observer forKey: @"app_observer"];
err = AXObserverAddNotification(observer, element,
kAXApplicationActivatedNotification, app);
NSLog(@"Error: %d", err);
}
void observerCallbackFunction(AXObserverRef observer, AXUIElementRef
element, CFStringRef notification, void
*refcon) {
NSLog(@"notification = %@", (NSString *)notification);
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.