Get item number of result of "choose from list"?

Can anyone tell me how to find the item number of the item that a user selects from “choose from list”? I am asking because this is what I want to accomplish:

I am working on a script that asks the user to choose from a list of applications. The way it works is this: I use the “AllApplications” executable by regulus6633 (http://www.hamsoftengineering.com/codeSharing/AllApplications/AllApplications.html) to return a plain-text list of the POSIX paths of the applications that can open a file. I then coerce this into an AppleScript list, named “pathList”.

I then copy this list to another list, named “appList”, and trim the POSIX paths so that only the application name is left in each item. In other words, I change /Applications/TextEdit.app to TextEdit. I then display this list to the user and ask him to select the one he wants to use.

Now, if the user selects TextEdit, and it is item 5 on appList, I want to assign the POSIX path of TextEdit, which is item 5 on pathList. Is there an easy way to match the item numbers?

Thanks for any help?

My own scheme isn’t to build a list with filenames but a list with

01 filename1
02 filename2
03 filename3
.
25 filename25

So, it’s easy to know the index of the selected items


set the_index to (text 1 thru 2 of the_selected_item) as integer

With your list structure, you must use a loop to retrieve the index of the selected item in the list of name


repeat with i from 1 to count of file_names
if the_selected_item = item i of file_names then exit repeat
end repeat
-- here i is the index of the_selected_item
set the_selected_path to item i of path_names

Yvan KOENIG (VALLAURIS, France) samedi 5 mars 2011 18:50:37

Thank you, Yvan. I will try both those methods. This is exactly what I was looking for.

I like these handlers when I need to create then choose from numbered lists:


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

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

Useful as well when you want to choose from one list and recover data from another:

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

Since 1943/12/31, I am accustomed to my first name which is Yvan so, I’m in despair when you write it as Yves.

Yvan KOENIG (VALLAURIS, France) samedi 5 mars 2011 21:25:34

may be replaced by :


on leadingZeros(n)
return item -2 thru -1 of ("00" & n)
end leadingZeros

Yvan KOENIG (VALLAURIS, France) samedi 5 mars 2011 21:30:50

Oops! :slight_smile:


on leadingZeros(n)
	return text -2 thru -1 of ("00" & n)
end leadingZeros

Or my own favourite:


on leadingZeros(n)
	return text -2 thru -1 of (100 + n as text)
end leadingZeros

I feel the same way when someone calls me “Edmund” which happens all too often. My apologies! I’ve corrected the mistaken message!

Elegantly simple as always.

As you saw, I was really in despair and typed item when of course, text was needed.

I prefer “00”&n upon 100+n because “00”&n apply until 999. It just require to change the parameter defining the string length.


set nb_chiffres to 3 (*  
2 = allow 99 templates
3 = allow 999 templates *)

--.

set the_index to text -nb_chiffres thru -1 of ("00" & n)

Yvan KOENIG (VALLAURIS, France) dimanche 6 mars 2011 15:38:17

I like both forms depending on the class of the variable (text or number).
You can also use a variable string length with Nigel’s version


set nb_chiffres to 3 (*  
2 = allow 99 templates
3 = allow 999 templates *)

--.

set the_index to text -nb_chiffres thru -1 of (10 ^ nb_chiffres + n as text)

The result of the ‘^’ operator is a real, so you’ll also need a coercion to integer somewhere in the process:


set nb_chiffres to 3 (*  
2 = allow 99 templates
3 = allow 999 templates *)

--.

set the_index to text -nb_chiffres thru -1 of ((10 ^ nb_chiffres as integer) + n as text)

set nb_chiffres to 3 (*  
2 = allow 99 templates
3 = allow 999 templates *)
set n to 9
--.

set the_index to text -nb_chiffres thru -1 of (((10 ^ nb_chiffres + n) as integer) as text)

If you fail to coerce as integer, you will get 9.0 when you want 009

I don’t use 10^n because as far as I know, calculations upon floating values are slow.

Yvan KOENIG (VALLAURIS, France) dimanche 6 mars 2011 16:53:23

An alternative, of course, would be to use positive indices in the range reference: :slight_smile:


set nb_chiffres to 3 (*  
2 = allow 99 templates
3 = allow 999 templates *)
set n to 9
--.

set the_index to text 2 thru (nb_chiffres + 1) of ((10 ^ nb_chiffres + n) as text)

. although that doesn’t work when nb_chiffres > 3 because of the scientific notation.

Mmmm. Possibly minutely less mindbogglingly fast than integer maths ” and the coercions from integer to real for the calculation and then back to integer would add a little to the time; but I imagine the process would still be faster than a text concatenation. :wink:

In fact, I’m lazy.

I use the tip to build invoices number which must have six or even ten digits.
In these cases, the concatenation is the unique usable scheme.
So I use it in every cases.

In my collection of handlers it’s coded with “0000000000” & n

Sometimes I leave the ten zeroes, sometimes I drop some of them :wink:

Yvan KOENIG (VALLAURIS, France) dimanche 6 mars 2011 18:53:34