• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Eaves dropping on application events
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.
References: 
 >Re: Eaves dropping on application events (From: Joshua Kifer <email@hidden>)
 >Re: Eaves dropping on application events (From: Joshua Kifer <email@hidden>)
 >Re: Eaves dropping on application events (From: Eric Schlegel <email@hidden>)

  • Prev by Date: Re: Crash on NSMenu
  • Next by Date: OT: Seed installation strategy
  • Previous by thread: Re: Eaves dropping on application events
  • Next by thread: Re: Eaves dropping on application events
  • Index(es):
    • Date
    • Thread