Discover the current user's screensaver timeout either programmatically or via commandline on a mac

StackOverflow https://stackoverflow.com/questions/20897862

Вопрос

Does anyone know how to discover what the timeout is for the screensaver of the current user on a mac. I would prefer a programmatic method if possible but am also happy to parse the output of a command line utility. I am already doing this with pmset to get the sleep times.

Это было полезно?

Решение

The value for screensaver settings for current user in (at least from 10.6.x upwards) stored in a property list file in ~/Library/Preferences/com.apple.screensaver.plist. It is possible that there is no value, in case the user has decided to disable automatic screen blanking/saving.

You can read the value with a command line utility called defaults(1):

defaults read com.apple.screensaver idleTime
defaults -currentHost read com.apple.screensaver idleTime

The former provides access to settings of current user, the latter to system defaults, should one exist.

Reading values with defaults(1) is somewhat complicated as the value may or may not be present. It'd be sane to assume that defaults read com.apple.screensaver idleTime would return zero in case the current user has disabled the screensaver, but that's not the case. Instead defaults(1) will toss an error message on the terminal and return non-zero exit code. Instead of just parsing the output of defaults(1), you'd have to first check the exit code and do parsing only in case it's zero. On the other hand, that might be a sane policy to follow anyway when parsing output of external commands.

Apple provides programming interfaces with their Xcode for reading/writing property lists as well.

Другие советы

As a follow-up to the correct original answer (in a response to the comments on that answer about El Capitan), it seems that the above property does not exist in 10.11 for the set of default values, but it does exist for currentHost:

$ defaults read com.apple.screensaver
{
    askForPassword = 1;
    askForPasswordDelay = 5;
    tokenRemovalAction = 0;
}

and then for currentHost:

$ defaults -currentHost write com.apple.screensaver idleTime 60
$ defaults -currentHost read com.apple.screensaver
{
    PrefsVersion = 100;
    idleTime = 60;
    moduleDict =     {
        moduleName = iLifeSlideshows;
        path = "/System/Library/Frameworks/ScreenSaver.framework/Resources/iLifeSlideshows.saver";
        type = 0;
    };
    showClock = 0;
    tokenRemovalAction = 0;
}

The above write command sets the idle time for my user, which is what I was looking for. Hope this helps!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top