First, you need to save the current setting, so you can put it back the way it was before you turned it off:
NSTask *readTask = [[NSTask alloc] init];
[readTask setLaunchPath:@"/usr/bin/defaults"];
NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"read", @"com.apple.screensaver", @"idleTime", nil];
[readTask setArguments:arguments];
NSPipe *pipe = [NSPipe pipe];
[readTask setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[readTask launch];
[readTask release];
NSData *data = [file readDataToEndOfFile];
NSString *originalValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
So now you have the original value for the screensaver's idleTime. Great! Don't lose that. Now, you have to set the new value:
NSTask *writeTask = [[NSTask alloc] init];
[writeTask setLaunchPath:@"/usr/bin/defaults"];
NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", @"0", nil];
[writeTask setArguments:arguments];
[writeTask launch];
[writeTask release];
And viola! You've just disabled the screensaver. To re-enable it, just use the second block of code again, but pass in originalValue as the last array object rather than @"0"
, like so:
NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", originalValue, nil]
Enjoy!
Billy
P.S.: One last thing, you may be tempted to save the NSTask objects to re-use them, but don't. They can only be run once, so you'll have to create new ones every time you want to do this.