Applescript&邮件中的规则

时间:2015-01-14 21:01:51

标签: applescript

我试图在我在Mac Mail中设置的规则中执行以下Applescript。我在iMac上运行OS 10.10.1 Yosemite。我的规则查找我每天收到的特定电子邮件,然后指示Mail执行我的Applescript。当我手动突出显示电子邮件并点击"应用规则"时,规则完美无缺。但是,当我早上启动计算机并收到当天的第一封邮件时。 Applescript程序陷入永无止境的循环中,这可以通过菜单栏中出现的旋转图标来证明。顺便说一句:我的自动变速器程序运行正常。我的想法是,当Mail正在下载我的所有邮件时,Applescript会因为执行而感到困惑。有什么建议??哦,我是新手... Thx

tell application "System Events"
    tell process "Mail"
        -- Select the Print menu item
        click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
        tell window 1
            -- Wait until the print sheet appears
            repeat until sheet 1 exists
            end repeat
            tell sheet 1
                -- Click the PDF button
                click menu button "PDF"
                -- Select the PDF to SBS Dropbox menu item
                click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
            end tell
        end tell
    end tell
end tell

1 个答案:

答案 0 :(得分:0)

首先,您需要使用邮件规则调用的Applescript处理程序:

on perform mail action with messages theMessages for rule theRule

在此处理程序中,您可以访问与您的邮件规则条件匹配的邮件,并执行您喜欢的任何操作。以下是可用于添加代码的框架:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with thisMessage in theMessages
                -- place your code here
            end repeat
        end tell
    end perform mail action with messages
end using terms from

在您的特殊情况下,我认为我们可以尝试将您的代码放入repeat循环中,让邮件在执行前打开邮件并在打印后关闭它:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"

            -- walk through all matching messages
            repeat with thisMessage in theMessages
                -- open the message
                set openedMail to open thisMessage

                -- perform your UI scripting
                tell application "System Events"
                    tell process "Mail"
                        -- Select the Print menu item
                        click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
                        tell window 1
                            -- Wait until the print sheet appears
                            repeat 30 times
                                if sheet 1 exists then exit repeat
                                delay 0.5
                            end repeat
                            tell sheet 1
                                -- Click the PDF button
                                click menu button "PDF"
                                -- Select the PDF to SBS Dropbox menu item
                                click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
                            end tell
                        end tell
                    end tell
                end tell

                -- close the message
                close openedMail

            end repeat
        end tell
    end perform mail action with messages
end using terms from

尚未测试!当我现在尝试访问打印页时,我阻止脚本在无限循环中运行。

试一试!享受,迈克尔/汉堡