Applescript: Comparing variable to string is failing

applescript

I have an applescript that gets a list of items and then iterates over them:

tell application "GeekTool Helper"
    set names to name of geeklets

    repeat with currentName in names
        if (currentName is equal to "Top_CPU_Processes") then
            display dialog "found it"
        end if
        return currentName
    end repeat
end tell

The names variable gets set to an array of strings properly. When I repeat over the list I'm able to get each of the currentName variables returned separately without an issue.

The problem that I'm running into is the if statement. I'm never getting the dialog box that display's "found it".

I've tried the comparison as if (currentName = "Top_CPU_Processes") then as well and it still never evaluates as true.

Is there something that I need to do to have the contents of the variable evaluate against the string?

Best Answer

When you have a list/array of variables and you iterate(repeat) over them, you are getting references to each item - such as "item 1 of names". In other words, you don't directly get the value, you get a pointer to the item in the array. You have to coerce the item reference to it's actual value to compare it.

Try: if ((currentName as string) is equal to "Top_CPU_Processes) then

Also, if you are using Script Debugger, it's a lot easier to see what goes on with the script, it has much much better debug capability than the Applescript Editor.