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