Selecting from a list

I am trying to activate an Excel workbook when a number are open. The first script does not work although the “Display Dialog” shows the name of the workbook I selected.

The second script does work but seems very clumsy forcing the routine to loop through the possibilities to get the index which only returns the same information as the first script. Would appreciate some direction if its possible to use the 1st script

[AppleScript]
if application “Microsoft Excel” is running then
tell application “Microsoft Excel” –
set ExcelWBName to the name of every workbook
set ExcelO to choose from list ExcelWBName
display dialog ExcelO
activate object workbook ExcelO
end tell
end if
[/AppleScript]

[AppleScript]
if application “Microsoft Excel” is running then
tell application “Microsoft Excel” –
set ExcelList to {}
set ExcelWBName to the name of every workbook
repeat with i from 1 to count of every workbook
set end of ExcelList to item i of ExcelWBName
end repeat
set ExcelO to choose from list ExcelList
repeat with i from 1 to count of ExcelWBName
if item i of ExcelWBName is ExcelO as string then
set ExcelO to item i of ExcelWBName
exit repeat
end if
end repeat
activate object workbook ExcelO
end tell
end if

This is a very common misunderstanding.

choose from list returns always a list – or false if the user presses the Cancel button – even if you don’t specify the with multiple selections allowed flag.

You have to handle the false case and then dereference the list

if application "Microsoft Excel" is running then
	tell application "Microsoft Excel"
		set ExcelWBName to the name of every workbook
		set ExcelO to choose from list ExcelWBName
		if ExcelO is false then return
		set chosenName to item 1 of ExcelO
		display dialog chosenName
		activate object workbook chosenName
	end tell
end if

Note: The applescript tags are lowercase.

Stefan

Once again thank you, I have learnt something new, hopefully I will remember that choosing from a list still returns a list, simple and just something that did not occur to me. Having used some more complex lists in the past, after your response I had look at them and in every case I had collected the items 1 2 etc, that should have given me a clue.

Anyway thank you

Hi MitchBVI.

If you make your [applescript] and [/applescript] posting tags lower case throughout, your code will appear boxed with clickable links as in Stefan’s reply.

Thanks Nigel I will try and remember that, I have been using the default tags and did not notice I had managed somehow to capitalize one of them,

Peter