Matching results from (choose list) to second list


set chosenShow to (choose from list seriesResultsNames with title "TV Show Maker 3.0" with prompt "Please select correct TV Show, then press OK." default items item 1 of seriesResultsNames)
set showID to ""
repeat with i from 1 to seriesCount
	if chosenShow is equal to (item 2 of item i of seriesResults as text) then
		set showID to item 1 of item i of seriesResults
		return
	end if
end repeat

I have a list of lists called seriesResults, each list in it has 3 items. seriesResultsNames is a list of item 2 from each list in seriesResults. So for example seriesResults is {{one, two, three}, {four, five, six}} and seriesResultsNames is {two, five}. so I have chosenShow to be the item the user picks from seriesResultsNames. Then I need to find out which one of the lists in seriesResults chosenShow is from so I can get item 1 of that list. So say user picks “five”, I want to match that up to item 2 of seriesResults and then get item 1 of item 2 of seriesResults, which is “four”. This is what I am trying to do with the repeat loop, with seriesCount being the number of items in seriesResults. But it does not seem to work and after it runs showID is always empty. What am I missing here? Or if there is a better way to match the answer up to a item in seriesResults let me know. Thanks in advance.

Hi.

‘choose from list’ returns a list of the chosen items, even when only one item has been chosen. You need to extract the item from this list and compare it, rather than the list, with the other items in the repeat.

The “Cancel” button in most dialogs generates a “User cancelled” error which simply stops the script. But ‘choose from list’ uniquely returns ‘false’. Your code should provide for this.

Also, I think you should probably use ‘exit repeat’ rather than the blank ‘return’.


set chosenShow to (choose from list seriesResultsNames with title "TV Show Maker 3.0" with prompt "Please select correct TV Show, then press OK." default items item 1 of seriesResultsNames)

-- Check for the "Cancel" button having been used.
if (chosenShow is false) then error number -128 -- Explicitly generate the "User canceled" error ” ie. stop the script.
set chosenShow to item 1 of chosenShow -- Otherwise extract the one item from the returned list.

set showID to ""
repeat with i from 1 to seriesCount
	if chosenShow is equal to (item 2 of item i of seriesResults as text) then
		set showID to item 1 of item i of seriesResults
		exit repeat
	end if
end repeat

Awesome thanks a lot. This is exactly what I was looking for.