Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers 2

up vote 6 down vote accepted

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

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'
share|improve this answer
    
Nice! Command line AppleScript is often a good solution to this sort of problem. –  Daniel Lawson 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
    
I have working machines running 10.7, 10.5, and 10.3, but am away from all of them at the moment. I'd love to take a look at it in that context, but can't until tonight or tomorrow. –  Daniel Lawson May 16 '12 at 13:58
    
@DanielLawson I have edited my answer to reflect the testing of 10.5.x 10.6.x 10.7.3. The command functioned for each OS. Thanks again. –  E1Suave May 16 '12 at 14:10
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
show 1 more comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.