applescript - 在定义的列表中使用条件系统

applescript - using conditional systems in a defined list

概览

代码: 它应该打招呼但没有

set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
    log theItem
    if theItem = "Hello" then
        say theItem
    end if
end repeat

但如果我这样做它就有效

set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
    log theItem
    if theItem is in "Hello" then
        say theItem
    end if
end repeat

使用条件语句的唯一方法是用“is in”来执行条件语句中的代码吗?

如果您的脚本只想检查“hello”是否在您的列表中,则不需要循环:

set myList to {"Hello", "Goodbye", "I must be going"}
if "Hello" is in myList then
    log "the list contains item 'hello'"
else
    log "no item found"
end if

如果你还想使用循环,可能主要原因是你想知道项目编号(?)。

set myList to { "Hello", "Goodbye", "I must be going"}
repeat with i from 1 to count of myList
    set theItem to item i of myList
    if theItem = "Hello" then exit repeat
end repeat
log "item " & i & " is my target !!"

最后,为了回答您最初的问题,Applescript 文档指出,如果“if”检查列表的项目,则它必须被称为 'contents of'(请参阅:Apple Documentation about Repeat with) 然后下面的脚本也在工作:

set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
    log theItem
    if (contents of theItem) is "Hello" then
        log "this is my item"
    end if
end repeat

repeat with theItem in myList循环中theItem实际上是对

的引用

AppleScript 以您的名义静默取消引用该对象平等检查除外

在这种特殊情况下,您必须使用 contents of

显式引用对象
set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
    log theItem
    if contents of theItem = "Hello" then
        say theItem
    end if
end repeat

这不会影响 repeat with i from 1 to (count myList) 语法,因为 item i of myList 隐式取消引用对象。

if theItem as text = "Hello" then

TheItem 指的是列表中的项目。 « 你好 » 是一条短信。