Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

getting the result from a list

Hi,


I'd like to get the result from a list in a choose from list object but instead of getting the string returned, I'd like to get the index selected.


I found some example like this one :

set theList to {"A", "B", "C", "D"}
set theChoice to (choose from list theList) as string
set n to 0
repeat with i from 1 to number of items in theList
  set x to item i of theList
  set n to n + 1
  if x is theChoice then exit repeat
end repeat
return n


But it's not a good idea to do that because if two items have the same name, the script failed to return the good index :

set theList to {"B", "B", "C", "D"}
set theChoice to (choose from list theList) as string
set n to 0
repeat with i from 1 to number of items in theList
  set x to item i of theList
  set n to n + 1
  if x is theChoice then exit repeat
end repeat
return n

Select the item 2 but index 1 is returned.


Thx a lot.

Posted on May 16, 2015 1:38 PM

Reply
3 replies

May 16, 2015 2:45 PM in response to Combo

Just prefix each list them with its ordinal number. So in your second example you'd have


{"1 B", "2 B", "3 C", "4 D"}


Then there will be no ambiguity.


But I really have to ask if you have multiple identical items in a list why would the user (or program) assume that one item is different then the other? Again in your example why would one assume that the two B's represent different things?

May 18, 2015 11:13 AM in response to GeoCo

Ok, thank you. It's what I did before you answer me : put an integer 1 to n in the string, getting the integer form the string returned, but it's pretty ugly...


It could be great to have the index of the choice of even better an object in a data property, like in other list component from other language / framework.


Is it possible to extend the choose from list component to add this property ?

getting the result from a list

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.