I want to test if a string contains either of two text items ("&" or " and "). I want the test to return True if the string contains either item and False if it does not. I am using this test to determine if the specified string (the name of person(s) entered into a form field) is the name of a single person or the name of a couple.
The problem: This code seems to randomly vacillate between returning True and False. I have tried testing the string directly, testing a variable reference to the string, checking different strings, and explicitly resetting the value of the tested variables before running the test.... BUT the returned boolean continues to be unpredictable.
What am I missing?? (If this is an issue from the limitations of AppleScript, I'm open to using JavaScript to check the string).
The code below should return True when the test is run on variable x and y but False when run on variable z (because the "and" of "Southerland" is not a standalone word).
set x to "John Jacob & Johanna Smith"
set y to "Kathy and Kurt Gallows"
set z to "Barbara Southerland"
set theName to x
set coupleIdentifiers to {"&", " and "}
if theName contains some text item of coupleIdentifiers then
set testIt to true
else
set testIt to false
end if
get testIt
CodePudding user response:
The problem is in your use of some text item of coupleIdentifiers
which randomly returns an item of coupleIdentifiers
. Try running just the following two lines of code several times:
set coupleIdentifiers to {"&", " and "}
some text item of coupleIdentifiers
Run it enough times and it will return both items, one at a time.
You need to use a different query, e.g.:
if theName contains "&" or theName contains " and " then
As in:
set x to "John Jacob & Johanna Smith"
set y to "Kathy and Kurt Gallows"
set z to "Barbara Southerland"
set theName to x
if theName contains "&" or theName contains " and " then
set testIt to true
else
set testIt to false
end if
get testIt