Getting Choices when items are not simple text (e.g. paths)

It often happens that you want to offer the user a choice from a list, but the list items are ungainly, like paths, for example. I usually handle that by offering a list that corresponds to the one in which I need the choice, the names of the files, for example, instead of their paths, and use the handler below to return the item numbers of the names that were chosen. I then use that list of numbers as indices into the original list.

-- an over-simplified sample:
set mainList to {"A", "B", "C", "D", "E", "F", "G", "H"} -- might be a list of long paths
set secondList to {"a", "b", "c", "d", "e", "f", "g", "h"} -- might be a list of file names of the paths
try
	set myMainChoice to item getChoice(secondList, "Choose a letter", "Letter Choice", false, "Cancel", "Choose") of mainList
	display dialog "You chose a lower case " & "\"" & myMainChoice & "\""
on error
	display dialog "You Cancelled"
end try

to getChoice(aList, aPrompt, aTitle, mults, CancelButton, OKButton)
	-- aList is the original list to be numbered, aPrompt is the prompt text, aTitle is the window title text, mults is true for multiple choices allowed, CancelButton and OKButton set the labels on the default Cancel and OK buttons.
	set numList to {}
	set chosen to {}
	-- Make a numbered list in "numList".
	repeat with k from 1 to count aList
		set end of numList to (k as text) & ") " & item k of aList
	end repeat
	-- Offer a choice from the numbered list
	set choices to choose from list numList with prompt aPrompt multiple selections allowed mults with title aTitle cancel button name CancelButton OK button name OKButton
	if choices is not false then -- i.e., something was chosen (empty selection defaults to false)
		-- Extract the numbers only from the choices found in list "choices".
		repeat with j from 1 to count choices
			tell choices to set end of chosen to (text 1 thru ((my (offset of ")" in item j)) - 1) of item j) as integer
		end repeat
		return chosen -- Return a list of item numbers for chosen list items
	else -- choices was false, the CancelButton button was chosen (always returns false no matter what it is called).
		return false -- pass back the false for main loop decisions.
	end if
end getChoice

very practical :slight_smile: