Monday, March 1, 2021

#1 2021-01-15 02:20:41 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 237

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
[/applescript]

Offline

 

#2 2021-01-15 03:02:26 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11739
Website

Re: Selecting from a list

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

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

Last edited by StefanK (2021-01-15 03:04:25 pm)


regards

Stefan

Offline

 

#3 2021-01-15 03:37:15 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 237

Re: Selecting from a list

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

Offline

 

#4 2021-01-15 05:02:49 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5357

Re: Selecting from a list

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.


NG

Offline

 

#5 2021-01-15 05:15:05 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 237

Re: Selecting from a list

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)