Our friendly and paranoid management have asked me to turn on the password access for screen savers across all our Macs. Not a big issue, but then they asked me to keep logs of how long the screen saver was running each time. Nosey!
Is this even possible?
Hi,
polling the screensaver engine process is quite expensive.
Using a faceless Cocoa application is a better solution.
It uses built-in notifications com.apple.screensaver.didstart and com.apple.screensaver.didstop
The log will be written into ~/Library/Logs/ScreenSaverLog.log and can be watched with Console.app
You can download it here
Note: The application runs in background and can only be terminated in Terminal or Activity Monitor.app.
Put it in startup items to launch it automatically at startup
If you’re interested in the code, here it is
SKScreenSaverLog.h
[code]#import <Cocoa/Cocoa.h>
@interface SKScreenSaverLog : NSObject {}
- (void)writeToLog:(NSString *)msg;
@end[/code]
SKScreenSaverLog.m
[code]#import “SKScreenSaverLog.h”
@implementation SKScreenSaverLog
-
(void) awakeFromNib
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenSaverDidStart:)
name:@“com.apple.screensaver.didstart” object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenSaverDidStop:)
name:@“com.apple.screensaver.didstop” object:nil];
}
-
(void)dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
-
(void)screenSaverDidStart:(NSNotification *)notification {
[self writeToLog:@“start”];
}
-
(void)screenSaverDidStop:(NSNotification *)notification {
[self writeToLog:@“stop”];
}
-
(void)writeToLog:(NSString *)msg
{
NSString *logName = [NSString stringWithFormat:@“Library/Logs/ScreenSaverLog.log”];
NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:logName];
NSFileHandle *aFileHandle;
NSCalendarDate *now = [NSCalendarDate calendarDate];
[now setCalendarFormat:@“%d.%m.%y %H:%M:%S”];
NSString *myString = [NSString stringWithFormat:@“%@ %@\n”, now, msg];
// check if the file exists
if(![[NSFileManager defaultManager] fileExistsAtPath:logPath]) {
[[NSFileManager defaultManager] createFileAtPath:logPath
contents:nil
attributes:nil];
}
aFileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
[aFileHandle seekToEndOfFile];
[aFileHandle writeData:[myString dataUsingEncoding:NSUTF8StringEncoding]];
[aFileHandle closeFile];
}
@end[/code]