If statement AppleScript";如果包含“;

If statement AppleScript";如果包含“;,if-statement,applescript,contain,If Statement,Applescript,Contain,我有一个脚本,它查找名称并搜索与另一个变量的匹配 但是,如果变量1是“Name Demo”,变量2是“Demo Name”,则脚本找不到匹配项 set nameMatchTXT to "" if NameOnDevice contains theName then set nameMatch to theName & " : Name Match" end if 不管顺序如何,是否需要更改此项以查找匹配项? PS脚本正在查找单词的通配符,有时处理双位字符会很困难。您必须对每个条

我有一个脚本,它查找名称并搜索与另一个变量的匹配

但是,如果变量1是“Name Demo”,变量2是“Demo Name”,则脚本找不到匹配项

set nameMatchTXT to ""
if NameOnDevice contains theName then
    set nameMatch to theName & " : Name Match"
end if
不管顺序如何,是否需要更改此项以查找匹配项?
PS脚本正在查找单词的通配符,有时处理双位字符会很困难。

您必须对每个条件进行单独检查。还有其他方法(例如复杂的正则表达式),但这是最简单、可读性最好的方法

set nameMatch1 to "Name"
set nameMatch2 to "Demo"
if (NameOnDevice contains nameMatch1) and (NameOnDevice contains nameMatch2) then
    set nameMatch to NameOnDevice & " : Name Match"
end if
如果要添加匹配条件,最终可能会添加更多。与其添加更多的变量和条件,不如将所有单词都放在一个列表中并对照检查。将来,如果需要添加更多单词,只需将该单词添加到列表中即可。为了便于阅读,我将其提取到单独的子程序中:

on name_matches(nameOnDevice)
    set match_words to {"Name", "Demo"}
    repeat with i from 1 to (count match_words)
        if nameOnDevice does not contain item i of match_words then
            return false
        end if
    end repeat
    return true
end name_matches


if name_matches(nameOnDevice) then
    set nameMatch to nameOnDevice & " : Name Match"
end if
澄清后编辑 如果您无法控制匹配的文本(如果它来自外部源,并且不是由您编码的),则可以将该文本拆分为单词,并将其用作第二个示例中的单词列表。例如:

on name_matches(nameOnDevice, match_text)
    set match_words to words of match_text
    repeat with i from 1 to (count match_words)
        if nameOnDevice does not contain item i of match_words then
            return false
        end if
    end repeat
    return true
end name_matches


if name_matches(nameOnDevice, match_text_from_some_other_source) then
    set nameMatch to nameOnDevice & " : Name Match"
end if
你的请求表明:

如果变量1是“Name Demo”,变量2是“Demo Name”,则 脚本找不到匹配项

这将解决这个问题:

set var1 to "Name Demo"
set var2 to "Demo Name"

if (var2 contains (word 1 of var1)) and (var2 contains (word 2 of var1)) then
    -- you have a match
    display dialog "var1 and var2 match"
else
    display dialog "no match"
end if

感谢您的回复,问题是,实际名称变量是自动从另一个脚本中获取的,它基本上是将全名放在一起的唯一问题是我有时会出现此错误“error”无法将“word 1 of\”转换为Unicode文本。“number-1700从”“word 1 of”“转换为Unicode文本”