Application activate and deactivate notification on Mac OSX 10.5

Discussion in 'Mac Programming' started by ruhi, Dec 7, 2009.

  1. ruhi macrumors member

    Joined:
    Jun 17, 2009
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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 :)
     

Share This Page