I want to be notified when the current application will change. I looked at NSWorkspace. It will send notifications only when your own application becomes active or loses the activity. I want to be informed about every application. How can I do this in Cocoa?
+6
A:
Thank you Jason. kEventAppFrontSwitched in Carbon Event Manager is the magic word
- (void) setupAppFrontSwitchedHandler
{
EventTypeSpec spec = { kEventClassApplication, kEventAppFrontSwitched };
OSStatus err = InstallApplicationEventHandler(NewEventHandlerUPP(AppFrontSwitchedHandler), 1, &spec, (void*)self, NULL);
if (err)
NSLog(@"Could not install event handler");
}
- (void) appFrontSwitched {
NSLog(@"%@", [[NSWorkspace sharedWorkspace] activeApplication]);
}
And the handler
static OSStatus AppFrontSwitchedHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
[(id)inUserData appFrontSwitched];
return 0;
}
cocoafan
2009-04-18 06:59:52
Yeah, I made a little example for somebody that actually posted notifications a while back, but I couldn't find it. You gave a nice summary, you should accept this answer :)
Jason Coco
2009-04-18 07:16:15
Remark: To successfully build an application using this, you have to add the Carbon and Core Services Frameworks to your build and include <Carbon/Carbon.h> and <CoreServices/CoreServices.h> in the implementation file that contains the handler.See http://stackoverflow.com/questions/801976/mixing-c-functions-in-an-objective-c-class/ on how to mix C with Objective-C
Christoph
2009-08-06 17:40:33
You rule. Thanks!
Enchilada
2010-04-11 15:55:24