Choose from list

Hi all,
I’m facing a new problem.
I have two txt files with the same line’s number. The first TXT contains names (one per line, return is the delimiter), the second one URLs. I would like to let the user choose from the list of names to open the related URL.
For example, the name list:

and the URLs list:

The user pick one element of the list (for example My photos) and Safari opens
I’m trying without success to get names list first:

set titleList to read "names.txt" as list using delimiter return
choose from list {titleList} with prompt "Choose a name:"

But the whole text file seems to be set as unique item of the list…


it’s less complicated

set titleList to paragraphs of (read file "path:to:names.txt")
choose from list titleList with prompt "Choose a name:"

Thank you very much!
I’m now facing two problems.

  1. I got an error running that code. Log tells that it cannot transform the list into string. Might it depend on the very long number of item listed? Consequently, can I limit the list to the first n paragraphs?
  2. How to configure an action on item selection?


If your file is a plain text file with one name per line, the code must work.

I answer by myself.

set titleList to paragraphs 1 thru 3 of (read POSIX file "names.txt")

EDIT: Now it works, there was a typo on second line. Now I have to open the related link from the second TXT when user pick an item… any hint?

I didn’t mention POSIX at all :wink:

Without POSIX I got an error “File does not exist”! :frowning:
I guess I have to return the paragraph number of the choosen item to look for same paragraph number into the other TXT file… is that the right way?

you should specify the path in HFS manner (diskname:file.ext).

Ok! :slight_smile:
And what about the second question? How to return the paragraph number of the choosen item?

you need a repeat loop to get the index number

set titleList to paragraphs of (read file ((path to desktop as text) & "names.txt"))
set chosenItem to choose from list titleList with prompt "Choose a name:"
if chosenItem is false then return
set chosenItem to item 1 of chosenItem
repeat with i from 1 to count titleList
	if item i of titleList is chosenItem then exit repeat
end repeat
display dialog chosenItem & " is item " & i & " of the list"

An alternative method that I like for choosing an item from one list and using it to target the item in a second list with the same position is shown in this example. Suppose I have a list of cities and a second list of city codes and I want to present the city names and retrieve the codes. This script does that by numbering the city list for presentation in a “choose from list”, then extracts the number chosen, and finally uses that number as the index of the desired item from the second list. As set up, it will function with up to 99 items. The leading zeros handler just makes the presentation look better by prepending a zero to items 1 through 9:

set Cities to {"Boston", "New York City", "Wash. DC", "Miami", "Chicago", "Denver", "Houston", "San Francisco", "San Diego", "Seattle"}
set CtyCodes to {"BOS", "NYC", "DCA", "MIA", "ORD", "DEN", "IAH", "SFO", "SAN", "SEA"}

to getchosenItemNums(aList)
	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 my leadingZeros(k) & ".  " & item k of aList
	end repeat
	-- Offer a choice from the numbered list
	set choices to choose from list numList with prompt "Hold the Command key down to make multiple choices." with multiple selections allowed
	-- 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 a list of item numbers
	return chosen
end getchosenItemNums

set Codes to {}
repeat with aCode in getchosenItemNums(Cities)
	set end of Codes to item aCode of CtyCodes
end repeat

on leadingZeros(n)
	if n > 9 then
		return n as string
	else if n < 10 then
		return "0" & n
	end if
end leadingZeros

Great, I have a lot to study now! :wink:
But a line in your code remembered me that a user could always select more then a single item from my list. How to prevent this? I want him to choose a single item as it will open a streaming link (I don’t want the media player to open more then one video).

Thanks in advance!


from the StandardAddition dictionnary (⇧⌘L)

choose from list[/b] list of number or string : a list of numbers and/or strings to display
[with title string] : the dialog window title
[with prompt string] : the prompt to be displayed in the dialog box
[default items list of number or string] : a list of items to initially select (an empty list if no selection)
[OK button name string] : the name of the OK button
[cancel button name string] : the name of the Cancel button
[multiple selections allowed boolean] : Allow multiple items to be selected?
[empty selection allowed boolean] : Can the user make no selection and then choose OK?

… just omit the property “multiple selections allowed”

set choices to choose from list numList with prompt "Please select a name."