Choosing from a List of non-unique items

Is there a way to get a user to choose an item from a list, and then get a unique return value based on which choice they picked?

using the following:

choose from list {“a”, “b”, “a”}

could return “a” with no way of determining which “a” was selected.

I’d rather do this without changing the list so that each element is unique. Any suggestions?

There are a number of different ways. What exactly is this a list of? Files, File Names, Folder Names, Paths, Strings?

If these are files then it might be good to repeat through the list and get the path to each and use that since that would guarantee a unique ID.

If these are just strings then perhaps you could repeat through each item and turn each one into a record such as {name:“a”, value:1}, {name:“b”, value:1}, {name:“c”, value:1}, {name:“a” value:2} etc

then work through a list of the records:


set theList to {{name:"a", value:1}, {name:"b", value:1}, {name:"c", value:1}, {name:"a", value:2}}
value of item 4 of theList

just a thought.

You have to do something to make the items unique. This is one way to do it:

set the_list to {"a", "b", "a"}
set choose_list to {}
repeat with i from 1 to (count the_list)
	set end of choose_list to (i as Unicode text) & ". " & item i of the_list
end repeat
set the_choice to (choose from list choose_list) as Unicode text
if the_choice = "false" then return
tell (a reference to my text item delimiters)
	set {old_tid, contents} to {contents, ". "}
	set {the_choice, contents} to {the_choice's text item 1, ", "}
	set {the_list_as_string, contents} to {the_list as Unicode text, old_tid}
end tell
display dialog "You chose item " & the_choice & " of {" & the_list_as_string & "}." buttons {"OK"} default button 1 with icon 1 giving up after 5
set the_choice to the_choice as integer
return item the_choice of the_list

Jon

Muad’Dib, using

set theList to {{name:"a", value:1}, {name:"b", value:1}, {name:"c", value:1}, {name:"a", value:2}}
choose from list theList

displays a lot of arbitrary unicode that is very difficult for a user to choose from.

Jon,
That’s basically what I’m currently doing, but I was looking for a way to do it while leaving the numbers transparent to the user.

Perhaps - with a little subterfuge…

on chooseIndex from l
	set d to text item delimiters
	set text item delimiters to ""
	set o to ASCII character 0
	set s to {}
	set r to {}
	repeat with n from 1 to count l
		set r's end to l's item n & s
		set s's end to o
	end repeat
	set i to choose from list r
	if i is false then error number -128
	set text item delimiters to o
	set n to count text items of i's item 1
	set text item delimiters to d
	n
end chooseIndex

chooseIndex from {"a", "b", "a", "b", "a", "b"}
--> [integer]

Thanks Kai