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