Applescript to Require Password in System Preferences

Hi everyone,

I have been trying to do something quite simple with Applescript to no avail:

I need a script that
1)Opens System Preferences
2)Chooses the Security Pane
3)Goes to the General Tab
4)If the "Require password" is checked, uncheck it
5)If the "Require password" box is unchecked, check it.

Does anyone know how to do it? It will save me a ton of time, since I do not want to have my computer locked when I am at home, but I do want to have it locked when I am out.

Also, if anybody could point me to a tutorial for system preferences applescript, that would be great. The dictionary on the applescript editor does not help that much 😟

Thank you all in advance! 🙂
Maria

A1278 Macbook Unibody, Mac OS X (10.6)

Posted on Feb 4, 2011 8:30 AM

Reply
4 replies

Feb 4, 2011 12:01 PM in response to MariaKinget

Hi Maria,

I don't think scripting the user interface is the best way to do this. You can set the preference in the screensaver's defaults directly. Below is an Applescript to toggle the setting.

Best wishes

John M

-- Toggle Screensaver Password --
-- John Maisey -- 4 Feb 2011 -- www.nhoj.co.uk
try
set myFlag to (do shell script "defaults read com.apple.screensaver askForPassword")
on error
set myFlag to 0
end try
do shell script "defaults write com.apple.screensaver askForPassword -int " & ((myFlag + 1) mod 2)

Feb 4, 2011 12:52 PM in response to MariaKinget

As for me, I like scripting the user interface:

*tell application "System Preferences"*
* reveal anchor "General" of pane id "com.apple.preference.security"*
* tell application "System Events" to tell process "System Preferences"*
* click checkbox "Require password" of tab group 1 of window "Security"*
* end tell*
quit
*end tell*


The above script seems to run in the background.

Of course, since the above script uses [GUI Scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html], you must enable the Accessibility Frameworks by clicking the checkbox labeled "Enable access for assistive devices" in the Universal Access System Preference pane.

See for example: http://www.macosxautomation.com/applescript/features/system-prefs.html.

Message was edited by: Pierre L (added “quit” and a few other things…)

Feb 4, 2011 12:56 PM in response to MariaKinget

-- hubionmac.com 04.02.2011

-- script that toggles "require password after standby / screensaver"-checkbox

-- Status is reported via dialog or growl if installed

-- links: http://hintsforums.macworld.com/showthread.php?p=591189

-- http://www.macosxautomation.com/applescript/features/system-prefs.html

-- http://growl.info/documentation/applescript-support.php

tell application "System Events"

tell security preferences

if (get require password to wake) = false then

set require password to wake to true

my display_message("Require password switched on", 2)

else

set require password to wake to false

my display_message("Require password switched off", 2)

end if

end tell

end tell

on display_message(msgTXT, msgTimeout)

tell application "System Events"

set isRunning to ¬

(count of (every process whose name is "GrowlHelperApp")) > 0

end tell

if isRunning = true then

tell application "GrowlHelperApp"

-- Make a list of all the notification types

-- that this script will ever send:

set the allNotificationsList to ¬

{"Status"}


-- Make a list of the notifications

-- that will be enabled by default.

-- Those not enabled by default can be enabled later

-- in the 'Applications' tab of the growl prefpane.

set the enabledNotificationsList to ¬

{"Status"}


-- Register our script with growl.

-- You can optionally (as here) set a default icon

-- for this script's notifications.

register as application ¬

"Finder" all notifications allNotificationsList ¬

default notifications enabledNotificationsList ¬

icon of application "Finder"



-- Send a Notification...

notify with name ¬

"Status" title ¬

"Status" description ¬

msgTXT application name ¬

"Finder"

return true

end tell

else

activate

display dialog msgTXT giving up after msgTimeout

end if

end display_message



I would prefere this in my case, if I want the user to enter a password....

do shell script "/System/Library/CoreServices/'Menu Extras'/User.menu/Contents/Resources/CGSession -suspend"



Message was edited by: hubionmac

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to Require Password in System Preferences

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.