Help with Lists

I’m trying to write a script to start a VNC connection to a remote mac.
I’ve sorted the VNC bit but I’m having a problem with Lists.

What I want to do is to be asked which room I want to connect to and then which mac. I thought I’d managed it but I keep getting an error on the second list which says “The variable result is not defined.”

here is my script:


set RoomList to {"1", "2", "3", "4"}
set MacList to {"Print Server", "eMac 01"}
set GalleryList to {"Gallery iMac"}
set EditList to {"FCE iMac 01", "FCE iMac 02"}
set PhotoList to {"Photo eMac 01", "Photo eMac 02"} -- Need REAL list

choose from list RoomList with prompt "Choose a Room."
copy the result as list to {the Choosen_Room}

if Choosen_Room is "1" then choose from list MacList with prompt "Choose a Mac."
if Choosen_Room is "2" then choose from list GalleryList with prompt "Choose a Mac."
if Choosen_Room is "3" then choose from list EditList with prompt "Choose a Mac."
if Choosen_Room is "4" then choose from list PhotoList with prompt "Choose a Mac."

copy the result as list to {the Choosen_Mac}

Can someone help me solve this error??

Browser: Safari 412
Operating System: Mac OS X (10.4)

I think the problem was in the comparison tests. The Ifs in the code below are structured with an if-then-else if layout and that section now also has and END IF, which may have been the issue. Hope this works for you. :slight_smile:


set RoomList to {"1", "2", "3", "4"}
set MacList to {"Print Server", "eMac 01"}
set GalleryList to {"Gallery iMac"}
set EditList to {"FCE iMac 01", "FCE iMac 02"}
set PhotoList to {"Photo eMac 01", "Photo eMac 02"} -- Need REAL list

choose from list RoomList with prompt "Choose a Room."
copy the result as string to {the Choosen_Room}
--note that this is being assigned as a string. Otherwise you might end up having issues with comparing a list to a string value...

if Choosen_Room is "1" then
	choose from list MacList with prompt "Choose a Mac."
else if Choosen_Room is "2" then
	choose from list GalleryList with prompt "Choose a Mac."
else if Choosen_Room is "3" then
	choose from list EditList with prompt "Choose a Mac."
else if Choosen_Room is "4" then
	choose from list PhotoList with prompt "Choose a Mac."
end if

copy the result as string to {the Choosen_Mac} --being assigned as a string here too.

Works a treat.
Thank you very much.

As a side note, you could also do it like this:

property roomNumbers : {"1", "2", "3", "4"}
property rooms : {¬
	{name:"Mac", computers:{"Print Server", "eMac 01"}}, ¬
	{name:"Gallery", computers:{"Gallery iMac"}}, ¬
	{name:"Edit", computers:{"FCE iMac 01", "FCE iMac 02"}}, ¬
	{name:"Photo", computers:{"Photo eMac 01", "Photo eMac 02"}} ¬
		}

choose from list roomNumbers with prompt "Choose a room:"
set theNumber to result

if result is not false then
	get computers of item ((first item of theNumber) as integer) of rooms
	
	choose from list (result) with prompt "Choose a Mac:"
	set Choose_Mac to result
end if

Nice one.

Much clearer than my first attempt.

Just to add to the range of options, the ‘choose from list’ command can accept and return numbers - so you could also do something like this:

set RoomList to {1, 2, 3, 4}
set MacList to {{"Print Server", "eMac 01"}, {"Gallery iMac"}, {"FCE iMac 01", "FCE iMac 02"}, {"Photo eMac 01", "Photo eMac 02"}}

try
	set Chosen_Mac to (choose from list MacList's item (choose from list RoomList))'s item 1
on error number -1728
	error number -128
end try

For the record, I used records instead of lists, thinking it could be easier to keep track of them that way. However, I was unaware of the use of error number -1728 in this context. Thanks for pointing that out.

I take your point about records, Bruce - especially on longer lists. :slight_smile:

Just to clarify for anyone that may not be familiar with it, error number -1728 (errAENoSuchObject) indicates a run-time resolution error - and usually occurs when an object beyond the currently available range is specified. For example:

try
	item 4 of {1, 2, 3}
on error e number n
	e & " (error number " & n & ")"
end try

In my suggestion, the first dialog (choose from list RoomList) should not error. However, if the user clicks the ‘Cancel’ button, this will return the Boolean value ‘false’. Since the value presented to the second dialog (choose from list MacList) should be an integer, the unexpected Boolean produces an error:

If the user chooses an item from the first dialog, but then clicks the ‘Cancel’ button of the second, the script tries to get item 1 of the result (which is a Boolean value, rather than the expected list). So this time, the error message is different:

However, since the error number is the same in either case, it can be re-mapped to error number -128 (userCanceledErr) to simulate a user cancellation - whether the user clicks ‘Cancel’ on the first or the second dialog.