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
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
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.
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.