Need help with script to change system preference!

I want my Macbook to require a password to wake from sleep when I am NOT at home, and to wake without a password when I am at home. I thought the easiest way to do this would be to have a script that checks what wireless network I am connected to and automatically make the change accordingly… I’m having some difficulties though. Here’s my script:


on run
	
	tell application "System Preferences"
		activate
		reveal anchor "Main" of pane "com.apple.preference.security"
		tell application "System Events" to tell process "System Preferences" to click checkbox 1 of window 1
		tell application "System Preferences" to set current pane to pane "com.apple.preference.security"
	end tell
	
end run

I’d like it to check the wireless network first, and if it is “wirelessxxxxx” then disable, otherwise enable. The problem I keep running into is that I am unable to compare a value against that of the ‘checkbox 1’ inside the security pane. How do I see whether the box is checked or not and then execute a command?

I don’t have a wireless network, but finding out if that box is checked is just a matter of asking for its value (0 if not checked, 1 if checked)

tell application "System Preferences"
	activate
	reveal anchor "Main" of pane "com.apple.preference.security"
	tell application "System Events" to tell process "System Preferences" to set V to value of checkbox 1 of window 1
end tell

Hi charlie,

try this:

set text item delimiters to {""}
set MACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether") as Unicode text -- get MAC address
set screensaverPlist to "~/Library/Preferences/ByHost/com.apple.screensaver." & MACaddress
set AirportOn to (do shell script "ping -c 1 -t 4 4.2.2.1 > /dev/null; echo $?") = "0" -- check whether Airport is on =0 on ; ≠ 0 off
set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
do shell script "defaults write " & screensaverPlist & " askForPassword -int " & ((not (AirportOn and SSID is "wirelessxxxxx")) as integer) as Unicode text

Thank you both! I did not know how to get the value of the checkbox, I kept calling it using a different method (the reverse of above) and wasn’t able to get anything.

@stefan - that’s a very elegant way of doing this. Nice. I am having a little trouble tho:

I get this error on run:

So I changed line 2 from

to

just to see the results. The script runs fine but I see no results with my password preference, regardless of whether I’m connected to my network or not. Any ideas what may be causing this?

a more robust way to get and format the MAC address is

set MACaddress to (do shell script "ifconfig en0 ether | awk '/ether/ {print $2}' | tr -d ':'")

The checkbox will be changed by the script, but to apply the changes it’s necessary to log out.
So changing the checkbox with GUI scripting works directly

set AirportOn to (do shell script "ping -c 1 -t 4 4.2.2.1 > /dev/null; echo $?") = "0" -- check whether Airport is on =0 on ; ≠ 0 off
set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
tell application "System Preferences"
	activate
	reveal anchor "Main" of pane "com.apple.preference.security"
	tell application "System Events" to tell process "System Preferences"
		if (value of checkbox 1 of window 1) is ((AirportOn and SSID is "wirelessxxxx") as integer) then click checkbox 1 of window 1
	end tell
end tell

If you follow the instructions in post #13 of this macosxhints forum thread, you can create a simple command file that will be used to tell the screensaver to re read the preferences.
This means you do not have to logout. or open the prefpane.

It should only take a couple of minute to do.
once done the script below should do what you want.
Note the command file is called “notif” , you will need to add the correct path to it in the script

set theWiFi to do shell script "system_profiler SPAirPortDataType|awk -F\": \" '/Current Wireless Network/{print $2}'" as string

set the_passw_state to (do shell script " defaults -currentHost read com.apple.screensaver askForPassword") as integer
if theWiFi is "wireless network" then
	if the_passw_state is 1 then
		set the_state to 0 as integer
		my lock(the_state)
	end if
else
	set the_state to 1 as integer
	my lock(the_state)
end if
on lock(the_state)
	do shell script "echo defaults -currentHost write com.apple.screensaver askForPassword -int " & the_state & " ;~/notif"
end lock

Hello (about 1 year on),

I see you guys have a solid understanding of apple scripting, I’m looking for something similar to this. I’d like to create a wireless setup script but so far I’ve haven’t gotten very far trying to script via GUI :

tell application "System Events"
		tell application "System Preferences"
			activate
			set current pane to pane "Network"
			delay 1
			reveal anchor "Advanced Airport" of pane "Network"
			delay 1

dot dot dot!

I’m having difficulty addressing the areas of the Advanced Airport page I’d like ‘clicked’ but I see being discussed here a script using a more UNIX oriented method.
How easy is it to do and could someone give me some pointers?

Best regards,

Si

This one works for setting up a WEP wireless network on 10.5.2. Been working on a WPA one, but keep getting stuck setting the certificate trust.


set WEPNameString to "SSID name"
set WEPPassString to "WEP Password"
set ClearWEPString to "Random text to clear out the clipboard for security purposes, nice place for an Easter Egg"

try
	tell current application
		activate
		set acctBox to display dialog "Click OK to install XXXXX wireless. Then please DO NOT TOUCH the keyboard or mouse/trackpad until wireless setup is complete!" buttons {"Cancel", "OK"} default button 2
		set myButton to the button returned of acctBox
		if myButton is "Cancel" then
			quit
		end if
	end tell
	tell application "System Preferences"
		activate
		set the current pane to pane id "com.apple.preference.network"
		get the name of every anchor of pane id "com.apple.preference.network"
		reveal anchor "Advanced Airport" of pane id "com.apple.preference.network"
		tell application "System Events"
			tell application process "System Preferences"
				click button 1 of group 1 of tab group 1 of sheet 1 of window "Network"
				set the clipboard to WEPNameString
				click text field 1 of group 1 of window 1
				keystroke "v" using {command down}
				click pop up button 1 of window 1
				click menu item "WEP Password" of menu 1 of pop up button 1 of window 1
				set the clipboard to WEPPassString
				keystroke "v" using {command down}
				delay 1
				set the clipboard to ClearWEPString
				click button "Add" of window 1
				click button "OK" of sheet 1 of window "Network"
				click button "Apply" of window "Network"
				do shell script "networksetup -setairportpower off"
				delay 1
				do shell script "networksetup -setairportpower on"
			end tell
			tell application "System Preferences" to quit
			
		end tell
		tell current application
			activate
			display dialog "Wireless Setup Is Complete!" buttons {"OK"} default button 1
		end tell
	end tell
end try