Hello.
This only works for N=1, to N=4, I suspsect I can change the adjustment of q so it is valid for larger sublists.
It is all curtsey of Nigel Garvey, he have given the idea, and the raw material so to speak for this. The intention is of course to return the first item with the sought. It will always return the first value, even if there are several instances, sharing that value.
Enjoy!
set a to {{"Apples", 1}, {"Melons", "2"}, {"Strawberries", "3"}, {"BlackBerries", "4"}, {"Blue Berries", "5"}, {"Pears", "6"}, {"Lemons", "7"}, {"Plums", "8"}, {"Bananas", "9"}, {"Mango", "10"}, {"Avocado", "11"}, {"Rasberries", "12"}}
set b to {{"Apples", "1", "fair"}, {"Melons", "2", "fair"}, {"Strawberries", "3", "fair"}, {"BlackBerries", "4", "fair"}, {"Blue Berries", "5", "fair"}, {"Pears", "6", "fair"}, {"Lemons", "7", "fair"}, {"Plums", "8", "fair"}, {"Bananas", "9", "fair"}, {"Mango", "10", "fair"}, {"Avocado", "11", "fair"}, {"Rasberries", "12", "fair"}}
set c to {{"Apples", "1", "fair", "Foreign"}, {"Melons", "2", "fair", "Seedy"}, {"Strawberries", "3", "fair", "Wild"}, {"BlackBerries", "4", "fair", "Foreign"}, {"Blue Berries", "5", "fair", "Foreign"}, {"Pears", "6", "fair", "Foreign"}, {"Lemons", "7", "fair", "Foreign"}, {"Plums", "8", "fair", "Foreign"}, {"Bananas", "9", "fair", "Berry"}, {"Mango", "10", "juicy", "Tasty"}, {"Avocado", "11", "nut", "eggsy"}, {"Rasberries", "12", "Cherries", "frabolicious"}}
set res to indexOfItemN("Rasberries", a, 2)
set res to indexOfItemN("Plums", b, 3)
on indexOfItemN(item_a, the_list, n)
” © McUsr and put in Public Domain. Don't post this as it is elsewhere, or put it into a Public Accessible Repository or Library
” You may use it as you see fit otherwise, but please refer to this post for reference: http://macscripter.net/edit.php?id=155921
local astid, p, q
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set the_list_as_string to return & the_list & return
set AppleScript's text item delimiters to return & item_a & return
if (the_list_as_string contains result) then
set p to (count paragraphs of text item 1 of the_list_as_string)
set q to p mod n
if n = 3 and q > 1 then set q to q - 1
if n = 4 then
if q = 2 then set q to q - 1
if q > 2 then set q to q - (n - q) - 1
end if
set AppleScript's text item delimiters to astid
if (p is 0) then return 1 -- Catch modern paragraph count for empty text
return (p div n) + q
else
set AppleScript's text item delimiters to astid
return 0
end if
end indexOfItemN
Edit
Ran back, reset text item delimiters, for the case when we don’t find the substring.