And You Thought c++ Was Bad
UPDATE: this has been solved, thanks to Anonymous posting this link in the comments. It turns out it was very much like what I was saying, it was checking a reference to the variable instead of its contents JUST like c++ does. This is a case of there being a “proper” way to use the language, but many of the edge cases work fine so the web is swamped with examples that work through a loophole. I’m not really sure how to address this suckiness, because I will now be using “contents of” everywhere, which just made the language a bit more verbose for me. I think that Applescript has simply not lived up to the elegance that HyperTalk had, and that’s why I’m so hard on it. I want it to be good so bad :-( Oh well, thanks to everyone for your feedback and verifications.
If you are using a Mac, try the following script in AppleScript Editor:
set theItems to {"hello"}
repeat with i from 1 to count of theItems
display dialog item i of theItems is "hello" -- true
--display dialog class of item i of theItems -- ctxt
end repeat
repeat with theItem in theItems
display dialog theItem is "hello" -- FALSE! #$@!
--display dialog theItem is equal to "hello" -- still false!
--display dialog theItem as string is "hello" -- this works...
--display dialog class of theItem -- ctxt, which is correct?!
end repeat
I lost over an hour to this bug today. I was getting the list of window names from a program and then trying to activate the window by iterating over the list and finding the one with the same name. I could make it work by hand-entering the string but not with the one in the list.
IMHO this bug is very serious because it indicates that under the hood, Applescript has some very serious problems. I think that this might be failing because Lion no longer runs PowerPC code, and Applescript was PowerPC for a very long time, so they probably recompiled Applescript as Intel and some new bugs were introduced. So it’s probably trying to use a reference to the item in the comparison, instead of the item itself, so the comparison fails.
Imagine the thousands of work hours lost globally to bugs like this. If you are registered on Apple’s developer site, please consider submitting a bug with me.
Thanks for your help!