Please critique my messy script.

Hey guys,

I just wrote a script that automates setting a few System Preferences, I’d like to know what you think.

As it is, the error logging doesn’t work very well, I’m not sure why, or if I’m even on the right track for catching errors with trys.

If you have any suggestions for making it more efficient, or work better, please let me know!

Thanks!

set errLog to ("Error Log" & return & (current date) & return & "--" & return)

-- Below are the fields you need to modify

set storeNumber to "034" -- Enter your store's number as three digits.
set targetCity to "Los Angeles - U.S.A" -- Enter your store's Time Zone as it appears in Date & Time.
set hourWake to "7" -- When the computer should start up in the morning, single digit for the hour.

--Initialization

tell me
	
	try
		set computerName to ¬
			(("ars" & storeNumber & ".") ¬
				& text returned of (display dialog ¬
				"Please enter the two digit number of this computer." buttons {"Cancel", "Continue"} default button ¬
				"Continue" default answer ¬
				"" with icon 1 ¬
				))
	on error
		my errorDialog("This application requires the number of this computer and cannot continue without it.")
		set errLog to (errLog & return & "Initial Setup - Computer number not given.")
		my errLogger(errLog)
		return
	end try
	
	try
		set mobilemePassword to ¬
			text returned of ¬
			(display dialog ¬
				"Please enter the store specific MobileMe password." buttons {"Cancel", "Continue"} ¬
				default button ¬
				"Continue" default answer ¬
				"" with icon 1 ¬
				with hidden answer)
	on error
		my errorDialog("This application requires the MobileMe password and cannot continue without it.")
		set errLog to (errLog & return & "MobileMe Setup - Password not given.")
		my errLogger(errLog)
		return
	end try
	
end tell


--Access for assistive devices

tell application "System Events"
	if UI elements enabled is false then
		
		tell application "System Preferences"
			activate
			set current pane to pane id "com.apple.preference.universalaccess"
			
			try
				display dialog ¬
					"This application requires access for assistive devices be enabled" buttons {"Cancel", "Enable"} ¬
					default button ¬
					"Enable" with icon 2
			on error
				my errorDialog("This application requires access for assistive devices be enabled and cannot run without it.")
				tell application "System Preferences" to quit
				return
			end try
			
		end tell
		
		tell application "System Events"
			activate -- Brings dialog to front.
			set UI elements enabled to true -- Brings up dialog, asks for password.
			
			if UI elements enabled is false then
				my errorDialog("This application requires access for assistive devices be enabled and cannot run without it.")
				tell application "System Preferences" to quit
				return
			end if
			
		end tell
		
	end if
end tell

-- Mobile Me

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.internet"
	reveal anchor "account" of current pane
	
	tell application "System Events"
		tell process "System Preferences"
			
			try -- enter MobileMe username
				set focused of text field 2 of group 1 of window 1 to true
				delay 0.5
				set value of text field 2 of group 1 of window 1 to the computerName
				delay 0.5
			on error
				set errLog to (errLog & return & "MobileMe Setup - Username not taken.")
			end try
			
			try -- enter MobileMe password
				set focused of text field 1 of group 1 of window 1 to true
				delay 0.5
				set value of text field 1 of group 1 of window 1 to the mobilemePassword
				delay 0.5
				keystroke return
				delay 3
			on error
				set errLog to (errLog & return & "MobileMe Setup - Password not taken.")
			end try
			
			try -- Switch to Back to My Mac and turn it off, then switch back to Accounts
				click radio button 4 of tab group 1 of window 1
				delay 3
				if title of button 1 of group 1 of tab group 1 of window 1 is "Stop" then
					click button 1 of group 1 of tab group 1 of window 1
					delay 3
				end if
				click radio button 1 of tab group 1 of window 1
			end try
			
		end tell
	end tell
end tell

delay 3

-- Date & Time

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.datetime"
	reveal anchor "TimeZone" of current pane
	
	tell application "System Events"
		tell process "System Preferences"
			
			try
				set value of combo box 1 of group 1 of tab group 1 of window 1 to the targetCity
				delay 1
			on error
				set errLog to (errLog & return & "Time & Date - Was not able to set Time Zone.")
			end try
			
		end tell
	end tell
end tell

delay 2

-- Sharing

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preferences.sharing"
	
	tell application "System Events"
		tell process "System Preferences"
			
			try
				set value of text field 1 of window 1 to the computerName
				delay 0.5
				keystroke return
				delay 1
			on error
				set errLog to (errLog & return & "Sharing - Was not able to set computer name.")
			end try
			
			try
				click checkbox 1 of row 3 of table 1 of scroll area 1 of group 1 of window 1
				delay 0.5
				click checkbox 1 of row 6 of table 1 of scroll area 1 of group 1 of window 1
				delay 0.5
				click checkbox 1 of row 7 of table 1 of scroll area 1 of group 1 of window 1
				delay 1
			on error
				set errLog to (errLog & return & "Sharing - Was not able to sharing options.")
			end try
			
			try
				click checkbox 2 of sheet 1 of window 1
				delay 0.1
				click checkbox 1 of sheet 1 of window 1
				delay 0.1
				click checkbox 3 of sheet 1 of window 1
				delay 0.1
				click checkbox 7 of sheet 1 of window 1
				delay 0.1
				click checkbox 5 of sheet 1 of window 1
				delay 0.1
				click checkbox 8 of sheet 1 of window 1
				delay 0.1
				click checkbox 9 of sheet 1 of window 1
				delay 0.1
				click checkbox 4 of sheet 1 of window 1
				delay 0.1
				click checkbox 6 of sheet 1 of window 1
				delay 0.1
				click button 1 of sheet 1 of window 1
			on error
				set errLog to (errLog & return & "Sharing - Was not able to set remote management options.")
			end try
			
		end tell
	end tell
end tell

delay 3

-- Screensaver

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.desktopscreeneffect"
	
	tell application "System Events"
		tell process "System Preferences"
			
			try
				click radio button 2 of tab group 1 of window 1
				delay 1
			end try
			
			try
				set value of slider 1 of group 1 of tab group 1 of window 1 to 360
			on error
				set errLog to (errLog & return & "Screensaver - Was not able to set value.")
			end try
			
		end tell
	end tell
end tell

delay 3

-- Enery Settings

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.energysaver"
	
	tell application "System Events"
		tell process "System Preferences"
			
			try
				click radio button 1 of tab group 1 of window 1
				delay 1
			end try
			
			try
				set value of slider 1 of tab group 1 of window 1 to 901
				delay 0.5
				set value of slider 2 of tab group 1 of window 1 to 901
				delay 0.5
				if value of checkbox 1 of tab group 1 of window 1 is 1 then
					click checkbox 1 of tab group 1 of window 1
					delay 0.5
				end if
			on error
				set errLog to (errLog & return & "Enery Saver - Was not able set values.")
			end try
			
			try
				click button 6 of window 1
				delay 1
				if value of checkbox 1 of sheet 1 of window 1 is 0 then
					click checkbox 1 of sheet 1 of window 1
					delay 0.5
				end if
				set value of pop up button 2 of sheet 1 of window 1 to "Every Day"
				delay 0.5
				set focused of UI element 10 of sheet 1 of window 1 to true
				delay 0.5
				keystroke "" & hourWake & ""
				keystroke tab
				keystroke "00"
				keystroke tab
				keystroke "AM"
				delay 0.5
				click button 2 of sheet 1 of window 1
			on error
				set errLog to (errLog & return & "Enery Saver - Was not able set schedule.")
			end try
			
		end tell
	end tell
end tell

delay 3

-- Software Update

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preferences.softwareupdate"
	
	tell application "System Events"
		tell process "System Preferences"
			
			try
				click checkbox 2 of tab group 1 of window 1
				delay 0.5
				click checkbox 1 of tab group 1 of window 1
				delay 0.5
				click button 1 of tab group 1 of window 1
			end try
			
			
		end tell
	end tell
end tell

delay 3


-- Finish it off

delay 2
tell application "System Preferences" to quit

-- Write out error log, if any.
on errLogger(errLog)
	tell application "Finder"
		set theLog to open for access ((path to desktop as Unicode text) & "Error Log") with write permission
		write errLog to theLog
		close access theLog
	end tell
end errLogger

if errLog is equal to (("Error Log" & return & (current date) & return & "--" & return) is false) then
	errLogger(errLog)
end if

-- Error dialog sub-routine

on errorDialog(errorMessage)
	
	tell me
		activate
		display alert ¬
			"Required information" message errorMessage ¬
			as warning ¬
			buttons {"Quit"} ¬
			default button ¬
			"Quit" giving up after 15
		
	end tell
	
end errorDialog

Hi,

some of the tasks you can perform directly with the command line.
For example

Software Update → /usr/sbin/softwareupdate
Energy Saver → /usr/bin/pmset
TimeZone, ComputerName → /usr/sbin/systemsetup (available only in Leopard)
Sharing Services → /sbin/service

read the corresponding man pages for further information

Yeah, I actually originally wrote a shell script which used defaults write, etc. The only problem with tools like pmset and a few others is that they must be run as root, and I’m trying to have as little user interaction as possible.

If I could I’d like to figure out how to change all of these preferences, it just seems like a pain pouring through plists trying to find them all.

You’re right, most of the commands require admin privileges, but
the mentioned shell commands change the parameters directly, there is no need to write into plist files.

For me the pain puzzeling GUI scripts is worse, also because the next system software update could change the GUI references.

Okay, so I’m following your awesome suggestion, and it’s working so far.

Please tell me, though, why my error logging attempts always fail!?

set arsNum to ("ars" & "034")
set cpuNum to "22"
set errLog to null
try
	do shell script "softwareupdate --schedule off"
on error
	my errLogger("Software Update", errLog)
end try

try
	do shell script "systemsetup -setsleep off"
on error
	my errLogger("Enery Saver", errLog)
end try

try
	do shell script "systemsetup -settimezone America/Los_Angeles"
on error
	my errLogger("Time Zone", errLog)
end try

try
	do shell script "systemsetup -setremotelogin on"
on error
	my errLogger("Remote Login", errLog)
end try

try
	do shell script "systemsetup -setlocalsubnetname " & arsNum & "-" & cpuNum
on error
	my errLogger("Local Network Name", errLog)
end try

try
	do shell script "systemsetup -setcomputername " & arsNum & "." & cpuNum
on error
	my errLogger("Computer Name", errLog)
end try

try
	do shell script "This will cause an error"
on error
	my errLogger("Error Causer", errLog)
end try

on errLogger(errName, errLog)
	set errLog to (errLog & return & "Setting " & errName & " produced an error.")
	return errLog
end errLogger

if errLog is not null then
	return errLog
end if

because the shell scripts dont throw errors, they return a status message like
Zurich/Australia is not a valid timezone. The command ‘listtimezones’ will show a list of valid time zones.