Hi all,
I had the following lines of code in a script:
set theList to words of "Connie Nichole"
repeat with wd in theList
display dialog wd
if wd is equal to "Connie" then
display dialog "sanity check passed!"
end if
end repeat
While the first display dialog displays “Connie”, the second display dialog isn’t coming up, meaning that the “wd is equal to Connie” part isn’t happening. What am I doing wrong?
set theList to words of "Connie Nichole"
repeat with wd in theList
display dialog wd
if contents of wd is "Connie" then
display dialog "sanity check passed!"
end if
end repeat
When you use the repeat with wd in theList form of repeat, wd is a reference to the contents, it is not the contents. For many operations, AppleScript will dereference it (obtaining the content), but for others like boolean tests, it will not, so you use “contents of”.
Ah. thanks much for clarifying that!
In this kind of repeat loop, the variable is actually a reference to a list item:
set theList to words of "Connie Nichole"
repeat with wd in theList
return wd -- return the first item
-- > item 1 of {"Connie", "Nichole"}
end repeat
If you run into problems with the loop variable, then you should try explicitly getting the contents of the reference.
set theList to words of "Connie Nichole"
repeat with wd in theList
display dialog wd
if contents of wd is "Connie" then
display dialog "sanity check passed!"
end if
end repeat
Side note: You might consider using starts with
or contains
:
repeat
display dialog "Enter some text: [Connie]" default answer ""
set {text returned:userInput} to result
if userInput contains "Connie" then
display dialog "sanity check passed!"
end if
end repeat
Edit: Beaten. :rolleyes: