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?

6
  • 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
    Commented May 16, 2012 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
    Commented May 16, 2012 at 10:17
  • Looks like Apple undermining their own plist mechanism. Commented May 16, 2012 at 10:19
  • 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
    Commented May 16, 2012 at 11:07
  • 1
    @macaco Can you please describe the method you used for monitoring file read/writes which toggling the 'ask for password' setting?
    – JS.
    Commented May 12, 2016 at 21:07

2 Answers 2

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'
7
  • Nice! Command line AppleScript is often a good solution to this sort of problem.
    – Daniel
    Commented May 16, 2012 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
    Commented May 16, 2012 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
    Commented Dec 19, 2012 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
    Commented Oct 18, 2015 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
    Commented Feb 1, 2018 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

2
  • 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
    Commented Dec 3, 2018 at 17:58
  • @TJLuoma that's because your build environment isn't configured correctly. llvm will report that error for every file you compile using frameworks. However the segmentation fault is due to a different issue. Looks like CFMessagePortCreateRemote is returning a null for the port. Hence SendRequest faults. I'm guessing that the remote Message Port name is invalid but I don't know what it should be. Commented Nov 3, 2020 at 6:27

You must log in to answer this question.

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