Toggle "ask for password after screensaver/sleep" or the delay in 10.7 using terminal

There's an option in the preference panel to change the time the mac is able to be in sleep/screensaver before requiring a password to be unlocked again.

I'm using OS X Lion 10.7.

Is there any way to change this setting using the terminal or an applescript? I tried to change the plist file using:

defaults write com.apple.screensaver askForPasswordDelay -int 60

also tried

defaults write com.apple.screensaver askForPasswordDelay -float 60

also completely disabling the password didnt work either

defaults write com.apple.screensaver askForPassword -int 0

The plist file was changed, but it had no effects at all. It's the same plist file that gets changed when manually switching the setting in the preferences.

Would be awesome if anyone got an idea how to fix my problem.

EDIT: also tried to: 1) add -currentHost flag 2) drop the -int / -float


You can do this using UI scripting. This requires enabled support for assistive devices in Universal Access preference pane. You can launch the script from the command line using osascript, but you need to have a GUI session for this to work.

Based on my older answer here, I created the following script which works on my File Vault enabled Lion. Apparently, a checkbox to disable the password altogether was removed, either by Lion itself or me enabling File Vault 2. In the latter case I cannot fix the script for you, but the linked one might work.

Change the index (6) of the menu item to click in the 9th line to select which of the options to choose.


enter image description here

tell application "System Preferences"
    set current pane to pane id "com.apple.preference.security"
    tell application "System Events"
        tell process "System Preferences"
            tell first window
                tell first tab group
                    click radio button 1
                    click pop up button 1
                    click menu item 6 of menu of pop up button 1
                end tell
            end tell
        end tell
    end tell
    quit
end tell

The following is the "official" method of changing this setting in AppleScript:

tell application "System Events" to set require password to wake of security preferences to false

It has two major problems:

  • It's boolean (you cannot change the grace period)
  • It doesn't work for me (it takes the place of the checkbox I don't have)

Try using the -currentHost option to the defaults command.

defaults -currentHost read com.apple.screensaver

defaults -currentHost write com.apple.screensaver askForPasswordDelay -int 60

In addition to the defaults command there's also /usr/libexec/PlistBuddy:

/usr/libexec/PlistBuddy -h

for f in ~/Library/Preferences/ByHost/com.apple.screensaver.*.plist; do
   /usr/libexec/PlistBuddy -c Print "$f"
done