I was wondering why this returns false instead of true.
set this_text to "<"
repeat with this_char in this_text
if this_char is equal to "<" then
display dialog "1" buttons {"OK"} default button 1
end if
end repeat
I was wondering why this returns false instead of true.
set this_text to "<"
repeat with this_char in this_text
if this_char is equal to "<" then
display dialog "1" buttons {"OK"} default button 1
end if
end repeat
this_char at that point is just a reference to your string which the repeat block has actually coerced to a list. If you want to compare apples to apples it’s good to be in habit of making sure your classes match first:
set this_text to "<"
repeat with this_char in this_text
if (this_char as string) is equal to "<" then
display dialog "1" buttons {"OK"} default button 1
end if
end repeat
You’re trying to use a repeat with a string. That doesn’t make any sense. What are you trying to achieve? Do you want to .
.see if a string matches another string?
set someText to "test"
if someText is equal to "test" then
return true
else
return false
end if
.see if a string contains a certain character?
set someText to "test>"
if someText contains ">" then
return true
else
return false
end if
.find the location of a certain character in a string?
set someText to "test>twice>"
return offset of ">" in someText
I was surprised that this is legal code. It turns out that it’s not exactly coercing the string to a list, but it’s cycling through each character of the string as if you had asked for every character of the string as a list:
set this_text to "hello"
repeat with this_char in this_text
this_char
end repeat
--> item 5 of "hello" ("o")
That said, I can’t imagine too many great uses for this behavior. Although it’s probably the simplest way to count how many times the letter S appears in “AppleScript is awesome!”
set the_count to 0
set the_text to "AppleScript is awesome"
repeat with the_char in the_text
if (the_char as string) = "s" then set the_count to the_count + 1
end repeat
the_count
--> 3
Which is interesting because it successfully ignores case, whereas the following will get the same count, but for the wrong reasons:
ignoring case
set the_text to "AppleScript is awesome"
set text item delimiters to "s"
set the_text to the_text's text items
set text item delimiters to ""
end ignoring
the_text
--> {"AppleScript i", " awe", "ome"}
As far as what this is useful for, I leave that up to you.
Actually a reference to an item of it.
Incorrect. The repeat block simply generates a reference to each item of the given value in turn. This value can be anything that has ‘item’ elements and responds to a ‘count its items’ command: a string or list object, or an appropriate application reference.
Roughly correct, although AppleScript is smart enough to ignore non-significant differences comparing strings against Unicode text or integers against reals.
Coercion is overkill. The simplest, most correct solution is simply to dereference the reference:
set this_text to "<"
repeat with char_ref in this_text
if contents of char_ref is equal to "<" then
display dialog "1" buttons {"OK"} default button 1
end if
end repeat