Vanilla AppleScript does not provide an indexOfObject
API. There are three ways:
- An index based loop which iterates the list and returns the index when the object was found.
- Bridging the list to AppleScriptObjC to use the
indexOfObject
API of NSArray
.
- If the list is sorted a binary search algorithm which is very fast.
A fourth way is to populate the list with this format
1 Foo
2 Bar
3 Baz
and get the index by the numeric prefix but this is quite cumbersome for large lists.
Edit:
There are a few issues in the code. The main two are that choose from list
returns a list (or false
) and you are comparing a string with the index variable, an integer.
Try this
set cities to {"New York", "Portland", "Los Angeles", "San Francisco", "Sacramento", "Honolulu", "Jefferson City", "Olympia"}
set city_chooser to choose from list cities
if city_chooser is false then return
set city_chooser to item 1 of city_chooser
repeat with i from 1 to (count cities)
set city to item i of cities
if city = city_chooser then exit repeat
end repeat
display dialog i
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…