11

I am trying to enable and disable the screensaver password requirement from the command line.

defaults read com.apple.screensaver

shows a variable askForPassword set to either 0 or 1, depending on whether I configured a password requirement in System Preferences or not.

defaults write com.apple.screensaver askForPassword 1

and

defaults write com.apple.screensaver askForPassword 0

enable and disable the password setting, or so I thought.

What I find instead is that the commands indeed check and uncheck the checkbox in System Preferences under Security but do not affect the screensaver at all.

If I enable the password in System Preferences and then disable it using the second defaults write command, the checkbox in System Preferences is unchecked, but the screensaver will still ask for a password. Only checking and unchecking the checkbox in System Preferences can change this behaviour now.

And if I disable the password in System Preferences and then enable it using the first defaults write command, the checkbox in System Preferences is checked, but the screensave won't ask for a password. Only unchecking and checking the checking the checkbox in System Preferences changes the behaviour afterwards.

What's going on?

I can imagine that this is a global setting and I should modify /Library/Preferences/com.apple.screensaver instead of the user domain. But in that case, why is there an effect on the System Preferences checkbox?

  • This is a little bewildering. I've watched file read/writes whilst toggling the 'ask for password' setting. The only file that I can see being modified is com.apple.screensaver. I'm guessing that a message is sent to some service when this button is toggled in the GUI as well as writing to the plist file. I'd wager that rebooting the system or logging out/in might cause the file to be reread by said service, making the desired change. – macaco May 16 '12 at 10:12
  • 1
    I was right! Logging out and then back in after changing the plist file causes the change in settings to be reflected. So, looks like you need to find which service is controlling the 'ask for password' behaviour and reset/reload it after modifying the plist. – macaco May 16 '12 at 10:17
  • Looks like Apple undermining their own plist mechanism. – Andrew J. Brehm May 16 '12 at 10:19
  • Ta. I hope someone will know that and answer here. – Andrew J. Brehm May 16 '12 at 10:20
  • 1
    It's the 'loginwindow' process that seems to access this file after it's been written by System Preferences. Which makes sense. Unfortunately, killing the loginwindow process will forcefully log you out. Keep digging! – macaco May 16 '12 at 11:07
7

If you are not forced to use defaults write you can use the following command. It interacts with the OS the same as if you were to utilize System Preferences.

TESTED ON:

  • 10.5.x
  • 10.6.x
  • 10.7.x
  • 10.8.x
  • 10.9.x

sudo osascript -e 'tell application "System Events" to set require password to wake of security preferences to false'

NOTE: If the command is being run inside of a script that has been given root privileges you would not need the sudo.

osascript -e 'tell application "System Events" to set require password to wake of security preferences to false'
  • Nice! Command line AppleScript is often a good solution to this sort of problem. – Daniel May 16 '12 at 12:37
  • @DanielLawson Thanks, are you currently working on 10.7? I generally like to post what OS's I have tested my commands on and unfortunately this morning I am stuck with an old Snow Leopard machine and won't have access to a 10.7 machine until later today. I would hate for it to function on 10.6.x and fail on 10.7 :–( However, I am fairly certain this will function as the plists are very similar. I know 10.5's screensaver.plist is different and some tweaking would be needed. Anyhow, thanks again. :–) – E1Suave May 16 '12 at 12:43
  • 1
    I have tested this on 10.7.5 on OS X Server and it does not work. The screensaver still requires a password and the preference is not unchecked. – user36938 Dec 19 '12 at 15:11
  • 1
    This works for me on 10.11 (El Capitan). Found it from this thread (github.com/dustinrue/ControlPlane/issues/421) – GTF Oct 18 '15 at 12:08
  • 1
    The "osascript" method does not work on my High Sierra Mac. The ~/Library/Preferences/com.apple.screensaver.plist file does not appear to be affected by the GUI switch on my High Sierra Mac. – Kent Feb 1 '18 at 19:50
3

I ran into a similar issue, and found a solution from user Guillaume on this forum post. Basically, you need to force the screensaver to reread the password requirement preference, which you can do with a C program:

#include <CoreFoundation/CoreFoundation.h>

int main(int argc, char ** argv)
{
    CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR("com.apple.loginwindow.notify"));
    CFMessagePortSendRequest(port, 500, 0, 0, 0, 0, 0);
    CFRelease(port);
    return 0;
}

And compile this with:

cc -o /tmp/anywhereyouwantit/notif notif.c -framework CoreFoundation

Then call this program immediately after your call to defaults write

  • Update: on High Sierra (10.13.6) this compiles, but reports this error: "ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation are out of sync. Falling back to library file for linking." It fails with a segmentation fault when run. – TJ Luoma Dec 3 '18 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .