13

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?

1
  • 2
    It is impossible to get this information from Cocoa. You have to use the Carbon Event Manager to get notifications on when a process other than your own becomes active. Commented Apr 18, 2009 at 6:14

2 Answers 2

26

If you're targeting 10.6 or later there's a notification for this:

// NSWorkspaceDidActivateApplicationNotification [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(foremostAppActivated:) name:NSWorkspaceDidActivateApplicationNotification object:nil]; 

Apple docs: http://developer.apple.com/library/mac/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-SW97

Sign up to request clarification or add additional context in comments.

2 Comments

Confirmed working in 2014 on OS X 10.9 (except I had to remove the colon in the @selector method). Thank you!
The colon is there because I expect your selector to look like this: - (void)foremostAppActivated:(NSNotification *)notification
13

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; } 

2 Comments

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 :)
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 stackoverflow.com/questions/801976/… on how to mix C with Objective-C

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.