Can't automate "Require password" via AppleScript

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?

Thanks,

bowjest

Hi,

try this (I tested it on Snow Leopard)


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

Hi Stefan,

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”.

I’m completely baffled.

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

Hi Stefan,

Thanks! The short answer is: because I didn’t know there was a simpler way to do this. :slight_smile:

I think I’ll see if I can turn this into an “if-then” statement so I can switch between the two.

All the best,

bowjest

this toggles the state

tell application "System Events"
   tell security preferences
       set require password to wake to not (get require password to wake)
   end tell
end tell

Wow! That’s outstanding! :slight_smile:

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.

Thanks,

bowjest

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

Thanks, Stefan.

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.

Thanks,

bowjest

then use this

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

Stefan, that’s really cool. Thanks.

I’ve definitely learned a lot today. Hopefully I can apply all this to a couple of other things.

Best,

bowjest

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
 

I do not know why this feature does not work.

Hi akim,

It works very well on my os10.9.

gl,
kel

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.

Hi akim,

Maybe the user needs administrative privileges.

gl,
kel

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.