AppleScript 问题检查指定文本的字符串

AppleScript issue checking string for specified text

我想测试一个字符串是否包含两个文本项(“&”或“和”)中的任何一个。如果字符串包含任一项目,我希望测试为 return True,如果不包含,则为 False。 我正在使用这个测试来确定指定的字符串(输入到表单字段中的人名)是一个人的名字还是一对夫妇的名字。

问题: 这段代码似乎在 return 真假之间随机摇摆。我已经尝试直接测试字符串,测试对字符串的变量引用,检查不同的字符串,并在 运行 测试之前显式重置测试变量的值....但是 returned布尔值仍然不可预测。

我错过了什么?? (如果这是 AppleScript 限制的问题,我愿意使用 JavaScript 检查字符串)。

当变量 xy 上的测试为 运行 时,下面的代码应该 return 为真,但是在变量 z 上 运行 时为假(因为“Southerland”的“and”不是一个独立的词)。

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

问题出在您对 some text item of coupleIdentifiers 的使用中,它随机 return 一个 coupleIdentifiers 项目。多次尝试 运行 以下两行 代码

set coupleIdentifiers to {"&", " and "}
some text item of coupleIdentifiers

运行 足够多次,它会 return 两个项目,一次一个。

您需要使用不同的查询,例如:

if theName contains "&" or theName contains " and " then

如:

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