I want to monitor screensaver and lockscreen events on an OSX box. As a first pass, I\'m fine with them just printing to the console.
Following the advice of another\'s
Edit: further testing showed that com.apple.screensaver.didlaunch
is also available, the code here is tested on 10.6.8 and 10.8.4
Your code will work if you remove [[NSRunLoop currentRunLoop] run];
and
initialize ScreenSaverMonitor from a Cocoa application's applicationDidFinishLaunching:
method. ( Just create a new Cocoa application project in XCode and add your code where appropriate ).
ScreenSaverMonitor.h
#import
@interface ScreenSaverMonitor : NSObject
-(id) init;
-(void) receive: (NSNotification*) notification;
@end
ScreenSaverMonitor.m
#import "ScreenSaverMonitor.h"
#import
#import
#import
#import
@implementation ScreenSaverMonitor
-(id) init {
NSDistributedNotificationCenter * center
= [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screensaver.didlaunch"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screensaver.didstart"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screensaver.didstop"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screenIsLocked"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screenIsUnlocked"
object: nil
];
return self;
}
-(void) receive: (NSNotification*) notification {
printf("%s\n", [[notification name] UTF8String] );
}
@end
AppDelegate.h
#import
#import "ScreenSaverMonitor.h"
@interface AppDelegate : NSObject
@property (assign) IBOutlet NSWindow *window;
@property (retain) ScreenSaverMonitor *monitor;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ScreenSaverMonitor.h"
@implementation AppDelegate
@synthesize monitor;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
self.monitor = [[ScreenSaverMonitor alloc] init];
}
@end
main.m
#import
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **)argv);
}