为什么AppleScript在Mac OS X 10.5 Leopard上出现“AppleEvent处理程序失败”错误?

时间:2011-07-29 16:32:26

标签: email applescript osx-leopard

首先,我知道,Leopard在这一点上是古老的,但它是我希望能够支持这个脚本的最小值,所以请耐心等待(在Mac OS X 10.4下甚至不可能)老虎由于bug w/Mail redirect/forward/reply bug I had uncovered long ago)。

我一直在重新构建AppleScript以与Topic Desk's spamtrainer一起使用,因此它会在Junk文件夹中选择一些消息并尝试将它们重定向到指定的邮箱。这是我到目前为止所做的事情(请注意,实际的发送目前已被注释掉,稍后会详细说明):

(* Train Spam - Redirect (not forward) email from the Junk folder (which hasn't already been redirected or
 *             determined by the mail server to be spam) to an appropriate spam mailbox on the mail server.
 *             Developed to be used in conjunction w/spamtrainer on a Mac OS X Server mail server.
 * 
 * v0.1   2011-07-27 - Morgan Aldridge
 *                     Initial version.
 *)

using terms from application "Mail"
    on perform mail action with messages selectedMsgs in mailboxes selectedMailboxes
        tell application "Mail"
            -- configuration variables
            set junkPrefix to "***JUNK MAIL***"
            set junkRecipient to "junkmail@domain.tld"

            -- ensure that we're in the Junk mailbox
            -- (it'd be disasterous to accidentally redirect mail for training from another mailbox)
            if (count of selectedMailboxes) is 1 and (first item of selectedMailboxes) is junk mailbox then
                set selCount to (count of selectedMsgs)
                set redirectedCount to 0
                repeat with counter from 1 to selCount
                    set msg to item counter of selectedMsgs
                    -- if the subject doesn't start with junkPrefix and the message hasn't already been redirected, then redirect it
                    -- (otherwise, if it starts with junkPrefix, it means it was already detected as junk by the mail server)
                    -- (and, obviously, if it was already redirected, that was probably for the sake of junk training as well)
                    if subject of msg does not start with junkPrefix and not was redirected of msg then
                        set newRedirectMsg to redirect msg with opening window
                        tell newRedirectMsg
                            -- set the to recipient to that of the specified spam mailbox on the mail server
                        make new recipient at beginning of to recipients with properties {address:junkRecipient}
                            -- remove any bcc or cc recipient (we don't want to be spamming anyone else in the process)
                            delete bcc recipients
                            delete cc recipients
                        end tell
                        -- actually send the message
                        -- send newRedirectMsg
                    else
                        display dialog "Oops, the message was already flagged as junk by the mail server or you've already redirected it!"
                    end if
                end repeat
            else
                display dialog "Oops, you're not in your Junk mailbox!"
            end if
        end tell
    end perform mail action with messages
end using terms from
-- this is required when _not_ running from the Script menu (e.g. Script Editor, FastScripts, etc.)
using terms from application "Mail"
    on run
        tell application "Mail" to set sel to selection
        tell application "Mail" to set selBox to selected mailboxes of message viewer 1
        tell me to perform mail action with messages (sel) in mailboxes (selBox)
    end run
end using terms from

Script Editor.app运行它(因此它通过on runtell me to)并在Mail中选择少量垃圾邮件(这是在Mac OS X 10.5.8下) Leopard),它似乎适用于第一条消息(打开新的重定向消息,w /到收件人集,CC / BCC字段清空),其他重定向消息窗口打开,但它们的To / CC / BCC字段不是更新并Script Editor.app弹出对话框,其中“Mail收到错误:AppleEvent处理程序失败”错误。事件日志的内容如下:

tell application "Mail"
    get selection
        {message id 464214 of mailbox "Junk", message id 464213 of mailbox "Junk", message id 464211 of mailbox "Junk"}
    get selected mailboxes of message viewer 1
        {junk mailbox}
    get junk mailbox
        junk mailbox
    get subject of message id 464214 of mailbox "Junk"
        ": Your Invitation Into a Global Directory"
    get was redirected of message id 464214 of mailbox "Junk"
        false
    redirect message id 464214 of mailbox "Junk" with opening window
        outgoing message id 400031520
    make new recipient at beginning of every to recipient of outgoing message id 400031520 with properties {address:"junkmail@domain.tld"}
        to recipient 1 of outgoing message id 400031520
    delete every bcc recipient of outgoing message id 400031520
    delete every cc recipient of outgoing message id 400031520
    get subject of message id 464213 of mailbox "Junk"
        "Nominate Your Favorite Products for the Community Choice Awards"
    get was redirected of message id 464213 of mailbox "Junk"
        false
    redirect message id 464213 of mailbox "Junk" with opening window
        outgoing message id 378471104
    make new recipient at beginning of every to recipient of outgoing message id 378471104 with properties {address:"junkmail@domain.tld"}
        "Mail got an error: AppleEvent handler failed."

所以,很明显,失败的原因是除了第一条消息之外的所有内容都设置了to recipients。在不重新启动Mail的情况下,后续运行脚本也会导致第一条消息失败。

这个问题最奇怪的部分是,在运行脚本后退出邮件时,会出现一个隐藏的空白窗口,用于每个重定向的消息(即使是成功的消息)弹出询问您是否要保存更改。这听起来非常类似于this thread中提到的内容,但我不确定是什么导致它。

那么,是什么导致了这个错误和失败?我该如何解决?并且,为了额外的信用,为什么创建隐藏的空白消息,如何防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

你的脚本对我来说很好。

如果我是你,我会尝试在同一台计算机上的另一个OS X帐户中运行该脚本,修复权限,查看你的日志(你已经完成了,但它仍然是一个好主意),运行所有相关的软件更新,暂时移动您的~/Library/Mail文件夹以将其作为一个因素消除等。告诉我发生了什么,特别是如果它再次起作用的话:):