[Noob] Getting the item number out of a list


sorry for the noob question, but I searched over the net for quite a while without finding any working results for what I’m looking for…

suppose I have a list {“one”, “two”, “three”}

I’d like to get 2 if I’m looking for the striing “two”, 1 for “one” and 0 for “four”.

i tried with:

set id to offset of “two” in myList

but it failed as it maybe works only within a single string!

Can someone help me?



Applescript has no built-in function for referencing the index of an object by comparison, but you can easily write a handler such as this that will do it…

set theList to {"one", "two", "three"}

getIndexOfObjectInList("two", theList)

to getIndexOfObjectInList(tmpObject, tmpList)
	repeat with tmpIndex from 1 to (count tmpList)
		if (item tmpIndex of tmpList) is tmpObject then return tmpIndex
	end repeat
	return 0
end getIndexOfObjectInList



Basically, what you’re looking for is a search algorithm. The first question you could ask yourself is how do you want to store the data that you’re searching for? There are many ways to store data. You could store your data in a file, in a variable, or have something else store your data.

There have been many posts on storing data in files.

There are many ways to store data in variables.

If you look at the overall thing, your question is what is the most efficient way to store values?

AppleScript is a very low level high level language. It is very limited. Using lists is very slow, so how can you get some speed. Use text. Some of the fastest ways to manipulate data in AppleScript is to manipulate text. There are two major methods to manipulate text in AppleScript.

The first method is to use the AppleScript property ‘text item delimiters’ an the second way is to use the ‘offset’ command.

There are many posts which deal with the text item delimiters property of AppleScript. A few posters have posted with the ‘offset’ method. The Offset method is good if you have unique search strings. On the other hand, using text item delimiters is good if you want to separate groups of text.

I gotta go.

Will continue maybe tomorrow.


I happened to have a list of 700 key - value pairs on which I should base a rename action… I ended up making a Foundation tool console program and call it via script to rename downloaded iPhoto pictures… it works nicely and is amazingly fast!

PS: Anyone noticed how scripting became slower in 6.x when compared to 5.x?