Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ruhi

macrumors member
Original poster
Jun 17, 2009
70
0
Hello,

There are notification available in OSX 10.6 :
NSWorkspaceDidActivateApplicationNotification
and
NSWorkspaceDidDeactivateApplicationNotification​

what can be done to get same kind of notification for MacOSX 10.5?

Thanks,
Ruhi.
 
I wrote an app to automatically track time spent in other apps ages back (OSX 10.4). I used this:

Code:
NSNotificationCenter *defaultNC = [NSNotificationCenter defaultCenter];
NSNotificationCenter *distributedNC = [[NSWorkspace sharedWorkspace] notificationCenter];

// Register for all sorts of notifications to note when apps change.
// This bit should work on OSX 10.3.9 (and below).  Might be overkill :)
[distributedNC addObserver:self selector:@selector(appDidChange) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[distributedNC addObserver:self selector:@selector(appDidChange) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
[defaultNC addObserver:self selector:@selector(appDidChange) name:NSApplicationDidBecomeActiveNotification object:NSApp];
[defaultNC addObserver:self selector:@selector(appDidChange) name:NSApplicationDidResignActiveNotification object:NSApp];
[defaultNC addObserver:self selector:@selector(appDidChange) name:NSApplicationDidHideNotification object:NSApp];
[distributedNC addObserver:self selector:@selector(appDidChange) name:@"com.apple.HIToolbox.menuBarShownNotification" object:nil];

// This bit works on OSX 10.4 but does not seem to work on 10.3.9 (or 
// probably before)
EventTypeSpec spec = { kEventClassApplication, kEventAppFrontSwitched };
OSStatus err = InstallApplicationEventHandler(NewEventHandlerUPP(handleAppFrontSwitched),1,&spec,(void*)self,NULL);
if (err) 
{
	// TODO: Should really do something better here.
	NSLog(@"Uh oh...");
}
	
// Register for sleep notifications.
[distributedNC addObserver:self selector:@selector(computerWillSleep) name:NSWorkspaceWillSleepNotification object:nil];
	
// Register for wake notifications.  Need to note that we are no
// longer sleeping and which app is now in use.
[distributedNC addObserver:self selector:@selector(appDidChange) name:NSWorkspaceDidWakeNotification object:nil];

Hope that helps :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.