I’ve got a script I found for turning the firewall on and off on my MBP and I’d like to modify it so I can quickly set the option to require password immediately after screensaver or sleep in System Preferences.
Here is what I have so far:
tell application "System Preferences"
activate
tell application "System Events"
tell process "System Preferences"
click menu item "Show All Preferences" of menu 1 of menu bar item "View" of menu bar 1
click button 6 of scroll area 1 of window "System Preferences"
click button 1 of window "Security & Privacy"
click checkbox "Require password:" of tab group 1 of window "Security & Privacy"
end tell
end tell
quit
end tell
It balks, however, at the line:
"click checkbox “Require password:” of tab group 1 of window “Security & Privacy”"
and produces the error:
error “System Events got an error: Can’t get window "Security & Privacy" of process "System Preferences".” number -1728 from window “Security & Privacy” of process “System Preferences”
I must be misunderstanding how some of the syntax works here, but for the life of me, I can’t see where.
Can anyone advise what I’ve mistyped or wrongly referred to?
tell application "System Preferences"
activate
reveal anchor "General" of pane "com.apple.preference.security"
end tell
tell application "System Events"
tell process "System Preferences"
click checkbox "Require password" of tab group 1 of window "Security"
end tell
end tell
quit
Thanks for your help. Unfortunately, it doesn’t work on my Lion system. I get:
error “System Events got an error: Can’t get window "Security" of process "System Preferences".” number -1728 from window “Security” of process “System Preferences”
If I change “Security” to “Security & Privacy” I get the same error, but with the window “Security & Privacy”.
The checkbox cannot be specified by name in Leopard
This should work
tell application "System Preferences"
activate
reveal anchor "General" of pane "com.apple.preference.security"
end tell
tell application "System Events"
tell process "System Preferences"
click checkbox 1 of tab group 1 of window "Security & Privacy"
end tell
end tell
quit
But why the long winded way at all, this does the same
tell application "System Events"
tell security preferences
set require password to wake to true
end tell
end tell
If I may ask, is there an easy way then, to do the same with this script for turning on/off my Wi-Fi?
do shell script “/usr/sbin/scselect Automatic”
I’ve got a feeling this would be a lot harder. I’ve never been able to come up with a solution, so I just made two apps (one on and one off) and put them in my dock.
Changing the current location can also be done with System Events
This toggles the current location between “Automatic” and “Whatever”
tell application "System Events"
tell network preferences
if current location is location "Automatic" then
set current location to location "Whatever"
else
set current location to location "Automatic"
end if
end tell
end tell
I’ve added the other profile (AirPortOff) to the script and the script completes, but it doesn’t actually switch profiles and thus turn off the connection:
tell application "System Events"
tell network preferences
if current location is location "Automatic" then
set current location to location "AirPortOff"
else
set current location to location "Automatic"
end if
end tell
end tell
The original script to turn it off is:
do shell script “/usr/sbin/scselect AirPortOff”
If I strip down the script above to just "set current location to location “AirPortOff” it also fails to change the connection state.
tell application "System Events"
tell network preferences to set currentLocation to name of current location
end tell
if currentLocation is "Automatic" then
do shell script "/usr/sbin/scselect AirPortOff"
else
do shell script "/usr/sbin/scselect Automatic"
end if
It appears that since I installed Mavericks 10.9, “require password to wake” of "security preferences "no longer toggles from true to false. If
is set to true, the following script cannot set it to false.
tell application "System Events"
get require password to wake of security preferences -- true
set require password to wake of security preferences to false
get require password to wake of security preferences -- still true, but should be false
end tell
After I read that the command is working elsewhere, I looked at the Systems Preferences pane, and noted that the “Require Password . after sleep or screen saver begins” is greyed out and cannot be manually unchecked. I guess this is not an AppleScript problem, but rather a problem elsewhere that has prevented any change in my Systems Preferences.Thanks for pointing out that the command is still operative.
Thanks, that was exactly which was needed. The applescript command works as it did before, now that the administrative privileges have been reset. Thanks for your help.