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

An AppleScript to toggle screen saver password Apps
If you want a quick way to toggle your screensaver password with Growl support (to know if it has been enabled or disabled), then try this AppleScript:

set output to do shell script "defaults -currentHost read com.apple.screensaver askForPassword -int"

if output is "0" then
  do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 1"
  tell application "GrowlHelperApp"
    set the allNotificationsList to {"Screensaver Setting"}
    set the enabledNotificationsList to {"Screensaver Setting"}
    register as application "AppleScript - SSPass" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "System Preferences"
    notify with name "Screensaver Setting" title "Screensaver Setting" description "The Screensaver password has been enabled." application name "AppleScript - SSPass"
  end tell
else
  do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 0"
  tell application "GrowlHelperApp"
    set the allNotificationsList to {"Screensaver Setting"}
    set the enabledNotificationsList to {"Screensaver Setting"}
    register as application "AppleScript - SSPass" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "System Preferences"
    notify with name "Screensaver Setting" title "Screensaver Setting" description "The Screensaver password has been disabled." application name "AppleScript - SSPass"
  end tell
end if
beep

[robg adds: Copy and paste the above into Script Editor and save it as an application. Double-click it and you should see Growl messages indicating the change in status (assuming you have Growl installed). Even slicker, use your favorite launcher app to assign a keyboard shortcut to run your script, and you'll have hotkey access to the toggle. This worked perfectly in my testing.]
    •    
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[15,119 views]  

An AppleScript to toggle screen saver password | 14 comments | Create New Account
Click here to return to the 'An AppleScript to toggle screen saver password' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Doesn't work for me...
Authored by: ChaChi on Jun 04, '07 09:33:31AM

All this does for me is just toggle the setting in System Preferences. It doesn't actually change whether or not my password is required. Can someone else test this and confirm it for me?



[ Reply to This | # ]
Doesn't work for me...
Authored by: Werd on Jun 04, '07 09:39:12AM

I get the same thing. The box gets checked and unchecked in the Security preference pane, but it doesn't seem to actually take effect until I log out and back in.



[ Reply to This | # ]
Doesn't work for me...
Authored by: ChaChi on Jun 04, '07 09:45:46AM

Thank you for confirming this Werd! I wish I knew what "magic" System Preferences performs when you physically click the button in Security. I've been trying to hunt down that answer for quite some time now!



[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: kdbdallas on Jun 04, '07 09:53:28AM
I have been working on another script to do just this, and while I personally like this one the part this is missing is telling the screen saver to use the new settings. The problem is that the screen saver process is already running and therefore it does not see the new changes. So what you have to do is tell the screen saver to get the new changes. I have a program to do this. While in my own testing this is not 100% effective all the time, I am hoping that with this new script it will be. If you are interested in the screen saver reset script you can find it on my site at: http://www.kdbdallas.com/index.php/2007/05/22/macos-x-screen-saver-password-onoff/ When I get a chance I will add this new code into my process and test it. If you are interested in what I find and what changes I do, make sure to check my site as I will post updates there.

[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: Synergy on Jun 04, '07 10:26:16AM

You need this program.

http://forums.macosxhints.com/showpost.php?p=335913&postcount=13



[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: kdbdallas on Jun 04, '07 11:45:00AM

My version mentioned above includes that program and I will still having issues, however I seem to have fixed it! The problem was that everything was running too fast. So if in between turning on/off the password you do a 3 second delay and then after running the notif program you do a 3 second delay then it works!



[ Reply to This | # ]
Why?
Authored by: ttt on Jun 04, '07 11:58:43AM

I don't know about other people, but I often find it really helpful if posters give scenarios that show how their hint is helpful. Just an abstract "if" doesn't speak to me as being useful. So "if" the intention is to provide quick access to a protected screen without having the screen saver password enabled all the time, try this:

Open Keychain Access (in /Applications/Utilities)
Go into it's Preferences
Under General, click Show Status in Menu Bar

Now any time you need to password protect your screen, use that menu to Lock Screen.



[ Reply to This | # ]
Why?
Authored by: Alrescha on Jun 04, '07 03:27:51PM

@ttt

Now *that* was useful...

Thanks!

A.



[ Reply to This | # ]
small screens...
Authored by: jspivack on Jun 05, '07 02:45:55AM

yes, that works, but those of us with 12" screens (PowerBook G4, anyone?) try to reduce the number of menu items running.



[ Reply to This | # ]
SWEET!!
Authored by: mizraith on Jun 06, '07 05:08:42PM

Often on OS X Hints it is not the hint that is the most useful, the the 5th or 6th reply below.

That Keychain Access hint was all I needed.... works perfectly and quickly.



[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: ajoslin on Jun 05, '07 06:45:38AM

I need a setting that says: only require password on wake after sleep

I've run the SleepWatcher tool to set the defaults listed above on sleep and wake but the settings didn't take unless I opened and closed the SysPrefs.

I like the idea of the notif script, but what is it really doing? And, is there a way to do the same thing without having to install a binary?

I support 60 mac laptop-toting folks and they just won't turn on the password on sleep, but I could enforce password on wake.



[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: designr on Jun 05, '07 10:48:44AM
set talkTRIES to 0
set TRYCOUNT to 250

tell application "System Preferences"
	activate
	set frontmost to true
	tell application "System Events"
		tell application process "System Preferences"
			click menu item "Security" of menu "View" of menu bar 1
			repeat until (window "Security" exists) or (talkTRIES > TRYCOUNT)
				set talkTRIES to talkTRIES + 1
			end repeat
			tell window "Security"
				if the value of first checkbox is 0 then
					click the first checkbox
					tell application "GrowlHelperApp"
						set the allNotificationsList to {"Screensaver Setting"}
						set the enabledNotificationsList to {"Screensaver Setting"}
						register as application "AppleScript - SSPass" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "System Preferences"
						notify with name "Screensaver Setting" title "Screensaver Setting" description "The Screensaver password has been enabled." application name "AppleScript - SSPass"
					end tell
				else
					click the first checkbox
					tell application "GrowlHelperApp"
						set the allNotificationsList to {"Screensaver Setting"}
						set the enabledNotificationsList to {"Screensaver Setting"}
						register as application "AppleScript - SSPass" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "System Preferences"
						notify with name "Screensaver Setting" title "Screensaver Setting" description "The Screensaver password has been disabled." application name "AppleScript - SSPass"
					end tell
				end if
			end tell
		end tell
	end tell
	quit
end tell


Admittedly more complex but this one works.

[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: TemporalKnot on Jun 13, '07 06:01:40AM

Doesn't work for me. On the line:
click menu item "Security" of menu "View" of menu bar 1

I get the following error :
System Events got an error: NSReceiverEvaluationScriptError: 4

I'm running 10.4.9 with all patches.



[ Reply to This | # ]
An AppleScript to toggle screen saver password
Authored by: pjrobertson on Dec 07, '08 03:53:19AM
I've come up with a solution similar to the above, that's a bit simpler and works better.
Check it out here:
http://tyddynadda.co.uk/design/toggle-screensaver-password-applescript/

[ Reply to This | # ]