One Quickie


Watching all notifications (Notifications->General)
There's three notification centers - one for the app, one for the workspace, and the distributed notification center. Here's how to watch everything:
#import <Cocoa/Cocoa.h>
#import <stdio.h> // for printf()

@interface NotificationSpy
{
}

+ (void) startSpying;
+ (void) stopSpying;

@end // NotificationSpy


// prevent us from adding ourselves multiple times

static BOOL g_spying;


@implementation NotificationSpy

// turn on listening for all notifications.

+ (void) startSpying
{
    if (!g_spying) {
        NSNotificationCenter *center;

        // first the default notification center, which is all
        // notifications that just happen inside of our program

        center = [NSNotificationCenter defaultCenter];
        
        [center addObserver: self
                selector: @selector(observeDefaultCenterStuff:)
                name: nil
                object: nil];

        // then the NSWorkspace notification center, which tells us things
        // like other applications launching, and the machine sleeping
        // and waking

        center = [[NSWorkspace sharedWorkspace]
                     notificationCenter]; 

        [center addObserver: self
                selector: @selector(observeWorkspaceStuff:)
                name: nil
                object: nil];

        // and lastly the distributed notification center.  This is a
        // global (to the computer) notification center.  You can find
        // out when a different program gets focus, or the sound or
        // screen brightness changes.

        center = [NSDistributedNotificationCenter 
                     notificationCenterForType: NSLocalNotificationCenterType];

        [center addObserver: self
                selector: @selector(observeDistributedStuff:)
                name: nil
                object: nil];

        g_spying = YES;
    }

} // startSpying 


// remove us as observers

+ (void) stopSpying
{
    if (!g_spying) {
        NSNotificationCenter *center;

        // take us off the default center for our app
        center = [NSNotificationCenter defaultCenter];
        [center removeObserver: self];

        // and for the workspace
        center = [[NSWorkspace sharedWorkspace] 
                     notificationCenter];
        [center removeObserver: self];

        // and finally off of the machine-wide center
        center = [NSDistributedNotificationCenter 
                     notificationCenterForType: NSLocalNotificationCenterType];
        [center removeObserver: self];

        g_spying = NO;
    }

} // stopSpying



+ (void) observeDefaultCenterStuff: (NSNotification *) notification
{
    // QuietLog can also be found in the quickies
    QuietLog (@"default: %@", [notification name]);
} // observeDefaultCenterStuff



+ (void) observeDistributedStuff: (NSNotification *) notification
{
    QuietLog (@"distributed: %@", [notification name]);
} // observeDistributedStuff



+ (void) observeWorkspaceStuff: (NSNotification *) notification
{
    QuietLog (@"workspace: %@", [notification name]);
} // observeWorkspaceStuff


@end // NotificationSpy



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com