MacOS – Disable screensaver password requirement from command line

defaultsmacosscreensaversystem-prefs

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?

Best Answer

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'