AppleScript中If语句的多个条件

AppleScript中If语句的多个条件,applescript,Applescript,我正在尝试修改一个applescript,当Outlook中有新邮件时,它会发出咆哮通知。原文是 在我的if语句中,我试图说,如果文件夹是已删除的邮件、垃圾邮件或已发送的邮件,请不要触发通知 声明如下: if folder of theMsg is "Junk E-mail" or "Deleted Items" or "Sent Items" then set notify to false else set notify to true end if applescript

我正在尝试修改一个
applescript
,当Outlook中有新邮件时,它会发出咆哮通知。原文是

在我的
if
语句中,我试图说,如果文件夹是已删除的邮件、垃圾邮件或已发送的邮件,请不要触发通知

声明如下:

if folder of theMsg is "Junk E-mail" or "Deleted Items" or "Sent Items" then
    set notify to false
else
    set notify to true
end if

applescript似乎不喜欢我添加的多个is/或项目。有没有一种方法可以包含多个条件,或者我是否需要编写嵌套的if/then?

在AppleScript中链接
if
条件的正确方法是重复完整的条件:

if folder of theMsg is "A" or folder of theMsg is "B" or folder of theMsg is "C" then
–左手论证没有隐含的重复。更优雅的方法是将左手参数与项目列表进行比较:

if folder of theMsg is in {"A", "B", "C"} then

这具有相同的效果(注意,这依赖于文本到列表的隐式强制,这取决于您的
告诉
上下文,可能会失败。在这种情况下,显式强制左侧,即
(作为列表的文件夹)
)。

当在条件语句中包含多个条件时,您必须重写整个条件语句。这有时会非常乏味,但这正是AppleScript的工作方式。您的表达式将变成以下形式:

if folder of theMsg is "Junk E-mail" or folder of theMsg is "Deleted Items" or folder of theMsg is "Sent Items" then
    set notify to false
else
    set notify to true
end if
不过,还有一个解决办法。您可以将所有条件初始化到列表中,并查看列表是否包含匹配项:

set the criteria to {"A","B","C"}
if something is in the criteria then do_something()
尝试:

尽管其他两个答案正确地解决了多个条件,但除非您指定MSG文件夹的名称,否则它们将不起作用

mail folder id 203 of application "Microsoft Outlook"

我通过谷歌搜索“applescript if multiple conditions”浏览了这篇文章,但没有浏览我在这里期待的代码片段,这就是我所做的(仅供参考):

您还可以递归地扫描多个条件。下面的例子是: -查看发件人电子邮件地址是否包含(Arg 1.1)内容(Arg 2.1.1和2.1.2),以立即停止脚本并“通知”=>true(Arg 3.1)。 -查看文件夹/邮箱(Arg 1.2)是否以“2012”开头(Arg 2.2.1),但不是文件夹2012-A B或C(Arg 2.2.2),如果它不是以2012开头或包含在三个文件夹之一中,则停止并不执行任何操作=>false(Arg 3.2)

--通过shell脚本执行多个条件比较

on _mc(_args, _crits, _r)
    set i to 0
    repeat with _arg in _args
        set i to i + 1
        repeat with _crit in (item i of _crits)
            if (item i of _r) as text is equal to (do shell script "osascript -e '" & (_arg & " " & _crit) & "'") then
                return (item i of _r)
            end if
        end repeat
    end repeat
    return not (item i of _r)
end _mc

if _mc({"\"" & theSender & " \" contains", "\"" & (name of theFolder) & "\""}, {{"\"@me.com\"", "\"Tim\""}, {"starts with \"2012\"", "is not in {\"2012-A\", \"2012-B\", \"2012-C\"}"}}, {true, false}) then
    return "NOTIFY "
else
    return "DO NOTHING "
end if
on _mc(_args, _crits, _r)
    set i to 0
    repeat with _arg in _args
        set i to i + 1
        repeat with _crit in (item i of _crits)
            if (item i of _r) as text is equal to (do shell script "osascript -e '" & (_arg & " " & _crit) & "'") then
                return (item i of _r)
            end if
        end repeat
    end repeat
    return not (item i of _r)
end _mc