11

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?

10

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.

  • Awesome, thanks for pointing that out. Also, good point re: using the return panel vs the display dialog. I was mostly doing that because I thought I was printing to the result panel incorrectly with return "found it" because it never printed. I'll return to return. – Chris Schmitz Feb 28 '14 at 15:03
  • Also, instead of coercing the reference, you can just dereference it (ask for its contents property): contents of currentName is equal to … See ASLG on reference or a reference to. – Chris Johnsen Mar 1 '14 at 10:27
  • Using the as string operator fixed the issue I was having – redolent Aug 10 '18 at 5:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .