Edit AllPages

Hi. I am trying to create a foundation tool that records workspace notifications (using General/NSLog()).

I am registering for notifications using:

General/NSNotificationCenter *center = General/[[NSWorkspace sharedWorkspace] notificationCenter]; [center addObserver:self selector: @selector(update:) name:nil object:nil];

This all works fine when the application stays alive (i.e. if I had a full blown General/NSApplication etc…). However, I am currently usure how to keep my “foundation tool” alive. I have looked into threads, run loops etc… but no topics seem to directly or indirectly apply.

Any help would be gratefully appreciated.

#import <Foundation/Foundation.h>

@interface General/MainObject : General/NSObject {

} @end

@implementation General/MainObject

int main(int argc, char *argv[]) { General/NSAutoreleasePool *pool = General/[[NSAutoreleasePool alloc] init]; General/NSRunLoop *loop = General/[NSRunLoop currentRunLoop]; General/MainObject *mainObject = General/[[MainObject alloc] init]; General/NSPort *port = General/[NSPort port]; [loop addPort:port forMode:General/NSDefaultRunLoopMode]; [loop configureAsServer]; [loop performSelector:@selector(start:) target:mainObject argument:port order:nil modes:General/[NSArray arrayWithObject:General/NSDefaultRunLoopMode]]; [loop run]; [pool release]; [mainObject release]; return 0; }


Thank you very much. My understanding of such matters is currently “very low” so please bare with me!

I am particularly interested in application opening and closing notices from the General/NSWorkspace notification center. However, when I replace General/[[NSDistributedNotificationCenter defaultCenter] addObserver… code with: General/[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(globalNotification:) name:nil object:nil];I receive no notifications. If I leave the code alone, I receive notifications but not the ones I am interested in.

Thanks for any further help!

The code above was just a starting point. If General/NSWorkspace’s notification center provides you with the info you need then use that instead of DNC.


Yes. The problem is that I cannot recieve notifications from the General/NSWorkspace notification center. I have included the General/AppKit framework and called General/NSApplicationLoad(), so General/[NSWorkspace sharedWorkspace] is available (I can see a list of running apps using one of its methods), however no notifications are being received. I am truly at a loss why this is the case. Could it be because I am not calling General/NSApplicationMain()? If so, I cannot see a way round the problem. Thanks again.

Try using General/[NSApp run] rather than [loop run].

Thanks! I shall go and learn something about run loops I think.

It’s not clear to me why you invoked “configureAsServer”. According to the General/NSRunLoop docs, “On Mac OS X, this method does nothing.”