问题 AppleScript中If语句的多个条件


我正在尝试修改一个 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

看来applecript不喜欢我添加的多个/或项目。有没有办法包含多个标准,还是我需要编写嵌套的if / then?


13136
2018-05-09 18:49


起源



答案:


正确的连锁方式 if AppleScript中的条件是重复完整的条件:

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

具有相同的效果(注意这取决于隐含的强制 文本 至 名单,这取决于你的 tell 上下文,可能会失败。在这种情况下,明确强迫你的左边,即 (folder of theMsg as list))。


14
2018-05-09 19:59





在条件语句中包含多个条件时,必须重写 整个条件。这有时非常繁琐,但这只是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()

1
2018-05-09 20:02





尝试:

repeat with theMsg in theMessages
        set theFolder to name of theMsg's folder
        if theFolder is "Junk E-mail" or theFolder is "Deleted Items" or theFolder is "Sent Items" then
            set notify to false
        else
            set notify to true
        end if
    end repeat

虽然其他两个答案正确地解决了多个标准,但除非您指定,否则它们将无法工作 name of theMsg's folder 或者你会得到

mail folder id 203 of application "Microsoft Outlook"

0
2018-05-09 20:08





通过谷歌搜索“苹果脚本如果有多个条件”并且没有出现在我希望的代码片段中,这就是我所做的(仅用于提供信息):

您还可以递归扫描多个条件。以下示例是: - 看是否 寄件人 电子邮件地址 包含 (Arg 1.1) 某物 (Arg 2.1.1和2.1.2)立即停止脚本并“通知”=> 真正 (Arg 3.1)。 - 看是否 文件夹/邮箱 (Arg 1.2) 从“2012”开始 (Arg 2.2.1)但是 不是文件夹2012-A B或C. (Arg 2.2.2)如果它不是从2012年开始或者包含在3个文件夹中的一个文件夹中停止并且不做任何事情=>  (Arg 3.2)。

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

- 通过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

https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW3


0
2017-07-11 20:59