Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Scripts to disable and enable password for lock screen after sleep or screen saver System

Here is two small scripts that I use to set and unset the password prompt when I lock the screen, or put my Mac to sleep. It is a nuisance to have to unlock the screen by entering the password, when at home. But I wouldn't leave my Mac anywhere without it.

I have called them setScreenPassword and unsetScreenPassword. I call them from QuickSilver, and you could use another launcher to do the same. The nifty thing about Quicksilver in this matter, is that if I activate it, I can see that I have turned on setScreenPassword, as it displays the last command before I start typing again.

I prefer to see the box in the System Preference pane get clicked, and unclicked. I have an old MacBook Pro, so I have added the delay of 0 so you can see where to add increments of 0.2 seconds, until you see the check box gets checked, or unchecked.

script EnablePasswordPrompt
	tell application "System Preferences"
		tell anchor "General" of pane "com.apple.preference.security" to reveal
		activate
	end tell
	tell application id "sevs"
		set UI elements enabled to true
		set a to value of checkbox 2 of tab group of window 1 of application process "System Preferences" as integer
		if a = 0 then
			tell checkbox 2 of tab group of window 1 of application process "System Preferences" to click
		end if
	end tell
	delay 0
	tell application "System Preferences" to quit
end script
tell EnablePasswordPrompt to run

---------------------------------------------
script disablePasswordPrompt
	tell application "System Preferences"
		tell anchor "General" of pane "com.apple.preference.security" to reveal
		activate
	end tell
	tell application id "sevs"
		set UI elements enabled to true
		set a to value of checkbox 2 of tab group of window 1 of application process "System Preferences" as integer
		if a = 1 then
			tell checkbox 2 of tab group of window 1 of application process "System Preferences" to click
		end if
	end tell
	delay 0
	tell application "System Preferences" to quit
end script
tell disablePasswordPrompt to run
    •    
  • Currently 1.60 / 5
  You rated: 2 / 5 (5 votes cast)
 
[10,995 views]  

Scripts to disable and enable password for lock screen after sleep or screen saver | 13 comments | Create New Account
Click here to return to the 'Scripts to disable and enable password for lock screen after sleep or screen saver' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: TheMacAdmin on Dec 07, '12 07:55:09AM

This hint uses UI scripting, which is unreliable and fails if you accidentally hit a key on the keyboard or brush the trackpad/mouse.

The same thing can be achieved with a one-line shell script.

defaults write com.apple.screensaver askForPassword -int 1

to undo it...

defaults write com.apple.screensaver askForPassword -int 0

The script can be rolled into an Automator action/app if you want to have a one-step process to change the setting. You could even create a one-app password toggler by using a "defaults read" command on the same preference key to fill a variable to determine the current state of the "require password" setting and then set it to the opposite value.

Personally I would never do this. Is the perceived inconvenience of entering your password at home worth the risk of forgetting to toggle the password lock, and thereby leaving your data vulnerable, while in public?



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: Lri on Dec 07, '12 08:14:42AM

checkbox 2 is "Disable automatic login" on my account (or 10.8). You don't have to activate System Preferences either.

tell application "System Preferences"
	reveal anchor "General" of pane "com.apple.preference.security"
end tell
tell application "System Events" to tell process "System Preferences"
	tell (checkbox 1 of tab group of window 1 where description starts with "Require password")
		click
		if value is {1} then
			set message to "Password enabled"
		else
			set message to "Password disabled"
		end if
		do shell script "terminal-notifier -message '' -title " & quoted form of message
	end tell
end tell
quit application "System Preferences"


[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: Lri on Dec 14, '12 04:59:54AM

I forgot that the setting can be changed directly: tell application "System Events" to tell security preferences to set require password to wake to not require password to wake



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: Asmus Vierck on Dec 07, '12 09:33:09AM
Why don't you use the following one-liner?
tell application "System Events" to set require password to wake of security preferences to true
Note: changes to this setting will only become visible in the system preferences app after relaunching it, but it will work instantly nevertheless.

[ Reply to This | # ]
Small change to LRI script
Authored by: jcooper11 on Dec 07, '12 12:55:35PM

LRI's script gave me an error of "terminal-notifier not found" for this line...
do shell script "terminal-notifier -message '' -title " & quoted form of message

Replaced it w/ this line...
display dialog quoted form of message



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: empyreal on Dec 07, '12 01:02:31PM

Asmus' script works fine for me. Simple, and saves disk space :-)

Running latest version of Mountain Lion, invoked with QuickSilver.



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: costee on Dec 07, '12 01:35:49PM

Is there any app, that records gui scripting like this?
Apple Script Editor doesn't work on System Preferences for example. I'd like to make a scroll direction switcher script.



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: regulus on Dec 10, '12 02:33:27PM
Yes, you can open Automator and add the action "Watch Me Do". Press record and it will record your actions. If you then want to move that to an applescript, copy the actions and paste into AppleScript Editor to see the applescript code... where you can then modify it however needed. I haven't used it much so I can't say if it works on system preferences either but it's worth a try. Good luck.
---
Hank
http://www.hamsoftengineering.com
Edited on Dec 10, '12 02:37:51PM by regulus


[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: Sesquipedalian on Dec 07, '12 06:13:08PM
Rather than relying on GUI scripting, this can be accomplished directly using the following AppleScript code:

Turn on password:
tell application "System Events" to set security preferences's require password to wake to true

Turn off password:
tell application "System Events" to set security preferences's require password to wake to false

Toggle password:
tell application "System Events" to set security preferences's require password to wake to not (security preferences's require password to wake)

[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: MacUser06 on Dec 07, '12 10:59:22PM

When it comes to turning on and off the screen password, the whole Idea is for the user to see it.

It is like if you are leaving your computer in a lab, with a locked screen while you are downloading, or merely going to lunch, or some stressed situation, when you are doing stuff while others are talking to you, then you have an affirmation, about what just happened. Leaving less doubt about what happened.

That is what I like about this script, and the intentions behind UI Scripting it. It is also nice to be able to toggle the password turning off and on quickly.

This line should really have been put in both scripts in the System Events block as the first statement, to make it work for those that haven't set the UI Scripting to true:


	tell application id "sevs"
		set UI elements enabled to true <— please insert this line
….
</code>
Edited on Dec 07, '12 11:03:56PM by MacUser06


[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: steresi on Dec 08, '12 05:20:29AM

I would prefer a way to require a password only when waking from sleep, but never when turning off the screensaver. When my computer's awake, I'm in front of it or it's at home. It's when it's asleep in my bag that it could get stolen! Anyone know of something that can do this?

Scott



[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: BlackPenguin on Dec 09, '12 07:05:13AM
This certainly would be a desirable function. I'd suggest trying any one of a number of tools which allow you to run an AppleScript at sleep/wake, and then run one of the scripts above to turn on security at sleep and off at wake. Take a look at one of these:

Sleep Watcher
ConrolPlane (née Marco Polo)
How to Run an AppleScript on Wakeup with Power Manager ($$)

[ Reply to This | # ]
Scripts to disable and enable password for lock screen after sleep or screen saver
Authored by: JimGrisham on Feb 10, '13 08:41:13PM

This script monitors whether the computer is currently plugged in; I use it to keep my screensaver password off at home and turn it on automatically when I'm out. It should be run as a login item. It may not work for you, but it works for me on Mac OS X 10.8. If you use it as a login item, the "MonitoringStatus" variable at the beginning must be set to TRUE; as it is written below it will run once and then ask if you would like it to continue running.

If you run the script on a desktop computer (anything without a battery) it will toggle the current password setting and then quit. You can have this behavior on a laptop by setting the "ScriptToggleOnly" variable to TRUE at the beginning of the script.

The script is similar to those above and was based on one by Patrick Robinson that was found at <http://patjack.co.uk/toggle-screensaver-password-applescript/>.

- Jim Grisham

-----------------

-- MonitoringStatus should initally be 0 unless the script is to be run as a login item
-- (in that case, set it to "True")
set MonitoringStatus to 0
set CurrentStatus to 0

-- Set ScriptToggleOnly to "True" to run in toggle mode on laptops
set ScriptToggleOnly to false

repeat
-- We only want to notify users if we actually change anything
set NotificationRequired to false

-- Check if security preference pane is open so we can refresh it later
set CurrentPrefPane to 0
if application "System Preferences" is running then
tell application "System Preferences"
try
set CurrentPrefPane to get id of current pane
end try
end tell
end if

-- Check if computer is plugged in
set PowerStatus to do shell script "pmset -g | grep Battery"
if first character of PowerStatus is "B" then
set ComputerType to "Portable
else
set ComputerType to "Desktop"
end if
if last character of PowerStatus is not "*" then
set PowerStatus to "AC Power"
else
set PowerStatus to "Battery"
end if

tell application "System Events"
-- Verify Growl is running to prevent a "Select application" dialog when we request a notification later
set isGrowlRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0

-- Change password requirement rules
tell security preferences
if ComputerType is "Portable" then
-- on a laptop, require password when on battery power
if PowerStatus is "AC Power" then
if CurrentStatus is "activated" then set NotificationRequired to true
set require password to wake to false
set CurrentStatus to "de-activated"
else
if CurrentStatus is "de-activated" then set NotificationRequired to true
set require password to wake to true
set CurrentStatus to "activated"
end if
else
-- Toggle current password setting if this is a desktop computer
set NotificationRequired to true
if get require password to wake is true then
set require password to wake to false
set CurrentStatus to "de-activated"
else
set require password to wake to true
set CurrentStatus to "activated"
end if
end if
end tell

end tell

-- Display Growl notification of new status (notify the first time, even if status has not changed)
if NotificationRequired is true or MonitoringStatus is 0 then
tell application id "com.Growl.GrowlHelperApp"
set the allNotificationsList to {"Screensaver Setting"}
set the enabledNotificationsList to {"Screensaver Setting"}
register as application ¬
"Auto-lock toggle script" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "System Preferences"
notify with name ¬
"Screensaver Setting" title ¬
"Screensaver Setting" description ¬
"Passwords have been " & CurrentStatus & "." application name "Auto-lock toggle script"
end tell
end if

-- Refresh System Prefrences app if security pane is currently displayed
if CurrentPrefPane is "com.apple.preference.security" and NotificationRequired is true then
tell application "System Preferences"
set current pane to pane id "com.apple.preference.general"
set current pane to pane id "com.apple.preference.security"
end tell
end if

-- if not a laptop, we're done
if ComputerType is not "Portable" or ScriptToggleOnly is true then exit repeat

-- should we keep running? (we only ask this once)
if MonitoringStatus is not "True" then
if button returned of (display dialog "Run in background to continue monitoring power status?" default button 1) is not "OK" then exit repeat
set MonitoringStatus to "True"
end if

-- wait four minutes and then check again
delay 60 * 4
end repeat



[ Reply to This | # ]