Paul
Paul

Reputation: 2738

How do I shut off the Mac screensaver?

I'm writing an application that uses Apple's kiosk mode. I would like to disable the screen saver, but the "ScreenSaverDefaults" class reports itself as being 32-bit only. I can change the build to be 32-bit only, but I would like to be able to support 64-bit architectures as well.

Are there any other Frameworks that I should use to disable the screen saver?

Upvotes: 4

Views: 1795

Answers (3)

Insomniac Software
Insomniac Software

Reputation: 293

For anyone searching for how to do this (like I have been doing) and don't want to mess around with editing the preference files, Apple has a proper method to stop the screen saver from starting up while your application is running.

Technical Q&A QA1160: Preventing sleep

Hope this helps.

Upvotes: 3

Paul
Paul

Reputation: 2738

What I ended up doing was directly reading the com.apple.screensaver preference file and modifying the idleTime and askForPassword values so that the are zero. A simple CFPreferencesSynchronize and all was well!

Upvotes: 1

vilhalmer
vilhalmer

Reputation: 1159

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.

Upvotes: 3

Related Questions