Scripting security preferences

I’d like to be able to toggle the “Require password x minutes after sleep or screen saver begins” option in the System Preferences Security pane. This does not work:

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

I have tried various permutations. The following works, but asks for a password for each item in the list:

tell application "System Events"
	tell security preferences
		set p to properties
		set require password to wake of p to (not require password to wake of p)
		set class of p to record
		set properties to p
	end tell
end tell

Any ideas? Is there a Terminal script that might accomplish this more easily? Thanks!

Hi,

setting and getting the same property in one line of code requires an explicit get command


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


Awesome. Thanks, Stefan!

My final code is this:

tell application "System Events"
	tell security preferences
		try
			set p to not (get require password to wake)
			set require password to wake to p
			if p is true then
				set onoff to "Off"
			else
				set onoff to "On"
			end if
			tell application "Finder"
				set name of file (path to me) to "Toggle Wake Password " & onoff
			end tell
		on error errmesg number errn
			display dialog errmesg & return & return & "error number: " & (errn as text)
		end try
	end tell
end tell

I saved this in the User Scripts Folder so I can quickly turn on the security setting when I go out to a coffee shop or on a trip.