Connection is invalid, Script Editor broken. Help please!!

Hello all,

I’ve been scripting all day and my scripts have all been debugged and were working perfectly until…

I tried running this script, and System Preferences seemed not to respond. After a timeout I got the error Connection is invalid. This is the same script that worked a few times over, why would it suddenly stop when i hadn’t changed anything?

So, I restarted my computer, and it still does not work. But now when I open the script in Script Editor it doesn’t have syntax highlighting, and replaced all the commands with weird <<>> things around them.

Below is my code, please tell me what I’m missing!

Thanks in advance!

-- Below are the fields you need to modify

set storeNumber to "XXX" -- Enter your store's number as three digits
set targetCity to "City Here - U.S.A" -- Enter your store's Time Zone as it appears in Date & TIme

--The following checks to make sure GUI scripting has access, and unlocks preferences

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 script requires access for assistive devices be enabled." & return & return & ¬
					"To continue, click the OK button and enter an administrative password in the forthcoming security dialog." with icon 1 default button 2
			on error number -128 -- If user cancels
				tell application "System Preferences"
					display dialog "This script requires access for assistive devices be enabled and cannot run without it." buttons {"Quit"} default button 1 with icon 2
					quit -- Quit system preferences
					return -- Quit this script
				end tell
			end try
		end tell
		tell application "System Events" to activate -- Brings dialog to front.
		set UI elements enabled to true -- Brings up dialog, asks for password.
		if UI elements enabled is false then -- Check to see if user cancelled, if so displays dialog and quits as above.
			tell application "System Preferences"
				display dialog "This script requires access for assistive devices be enabled and cannot run without it." buttons {"Quit"} default button 1 with icon 2
				quit
			end tell
			return
		end if
	end if
	
end tell

-- Initialization

tell application "System Preferences"
	
	set numberPrompt to display dialog "Please enter the two digit number for this computer." buttons {"Cancel", "Continue"} default button 2 default answer ("") with icon 1
	set computerNumber to ("ars" & storeNumber & "." & (the text returned of numberPrompt)) -- grab the text returned apart form button returned and store it
	
	set numberPrompt to display dialog "Please enter this store's MobileMe password." buttons {"Cancel", "Continue"} default button 2 default answer ("") with icon 1 with hidden answer
	set mobilemePassword to the text returned of numberPrompt -- grab the text returned apart form button returned and store it
	
	
end tell

tell application "System Preferences"
	launch
	
	-- Mobile Me
	
	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"
			
			if (exists checkbox "Click the lock to make changes." of window 1) is true then
				click checkbox "Click the lock to make changes." of window 1
				-- wait for security login or dismissal
				repeat until (count of every window) is not 0
					delay 1
				end repeat
				if (exists checkbox "Click the lock to make changes." of window 1) then
					set performChanges to false
				else
					set performChanges to true
				end if
			else
				set performChanges to true
			end if
			if performChanges is true then
				
				try
					set focused of text field 1 of group 1 of window 1 to true
					set value of text field 1 of group 1 of window 1 to the mobilemePassword
					keystroke return
				end try
				delay 4
				click radio button 4 of tab group 1 of window 1
				delay 0.5
				click button 1 of group 1 of tab group 1 of window 1
				
			end if
		end tell
	end tell
	
	-- Time Zone
	delay 1
	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"
			
			if (exists checkbox "Click the lock to make changes." of window 1) is true then
				click checkbox "Click the lock to make changes." of window 1
				-- wait for security login or dismissal
				repeat until (count of every window) is not 0
					delay 1
				end repeat
				if (exists checkbox "Click the lock to make changes." of window 1) then
					set performChanges to false
				else
					set performChanges to true
				end if
			else
				set performChanges to true
			end if
			if performChanges is true then
				
				set value of combo box 1 of group 1 of tab group 1 of window 1 to the targetCity
				delay 1
				keystroke tab
				keystroke tab
				delay 1
				
			end if
		end tell
	end tell
	
	
end tell -- End tell opening System Preferences

Hi,

I can’t test the script because I use Leopard and there the UI elements are different (there is no group 1 of window 1)
but two suggestions.

The display dialog lines didn’t work within the System Preferences tell block, so I changed to the Finder


.
tell application "Finder"
	activate
	set numberPrompt to display dialog "Please enter the two digit number for this computer." buttons {"Cancel", "Continue"} default button 2 default answer ("") with icon 1
	set computerNumber to ("ars" & storeNumber & "." & (the text returned of numberPrompt)) -- grab the text returned apart form button returned and store it
	
	set numberPrompt to display dialog "Please enter this store's MobileMe password." buttons {"Cancel", "Continue"} default button 2 default answer ("") with icon 1 with hidden answer
	set mobilemePassword to the text returned of numberPrompt -- grab the text returned apart form button returned and store it
end tell
.

For proper UI scripting the target process must be frontmost, so use activate instead of launch


.
tell application "System Preferences"
	activate
	
	-- Mobile Me
.

Thanks, I found that out eventually. I liked having system preferences display the dialog, it had to cool gear icon as icon 1 and that looked pretty neat. I realized, however, it made more sense logically to have these questions asked at script opening anyway, so I moved it to the start of the script and it’s asked by the script itself.

One thing bother me, though. I made this all in Leopard! Uh-oh!

What would you recommend using the get UI Elements?

Thanks!

I found out, the text field is only available, if you are not logged into mobileme yet.
The part of the script works fine on my machine, as mentioned above, I changed the launch command to activate.

I haven’t tested the timezone part yet