0

我有一个执行以下操作的 AppleScript。

  1. 激活 Mail.app。
  2. 在未标记的特定文件夹中查找消息(在我的用法中为“OmniFocus”)。
  3. 对这些消息运行脚本以将它们添加为 Omnifocus 中的任务。
  4. 标记每条消息。
  5. 将每封邮件移动到存档文件夹。

我添加了标记步骤(步骤 4),因为没有它,脚本会重复查找相同的消息,即使它们已被移动到存档文件夹。这导致了 OmniFocus 中的许多重复任务。该脚本有效,但是标记状态的使用是一种黑客行为,我想了解为什么 AppleScript 一直在我的“OmniFocus”文件夹中查找消息,而这些消息已经被移动到“存档”文件夹,所以我可以停止依赖在标记状态(并且只使用邮件所在的文件夹)来确定邮件是否已经被处理。

我在 2011 iMac 上运行 OS 10.8.3,我使用此脚本的邮件帐户是通过 FastMail 的 IMAP 帐户。

脚本如下。


property theAccount : "FastMail"

on run
tell application "Mail"
    launch
    synchronize with account theAccount

    set theOFFolder to mailbox "OmniFocus" in account theAccount
    set theArchiveFolder to mailbox "Archive" in account theAccount

    set theTempMessages to {}
    set theMessages to {}
    set theTempMessages to the messages in theOFFolder

    -- Remove the message from the list if it is flagged
    repeat with aMessage in theTempMessages
        if the flagged status of aMessage is false then
            set the end of theMessages to aMessage
        end if
    end repeat

    -- Quit if there are no messages to process
    set theMessageCount to count of theMessages
    if theMessageCount is equal to 0 then
        tell me to quit
    end if

    -- For each message, add it to Omnifocus, flag it, then move it to the FastMail archive folder
    try
        repeat with aMessage in theMessages
            my process_message(aMessage)
            delay 1
        end repeat
    on error m number n
        tell application "OmniFocus"
            log "Exception in Mail action: (" & n & ") " & m
        end tell
    end try

    repeat with aMessage in theMessages
        tell application "Mail"
            set flagged status of aMessage to true
            move aMessage to theArchiveFolder
        end tell
    end repeat
end tell

try
    tell application "OmniFocus"
        synchronize default document
    end tell
end try

tell application "Mail"
    synchronize with account theAccount
end tell

end run

on process_message(theMessage)
using terms from application "Mail"
    set theSubject to subject of theMessage
    set singleTask to false
    if (theSubject starts with "Fwd: ") then
        -- Whole forwarded messages shouldn't split.
        set singleTask to true
        set theSubject to rich text 6 through -1 of theSubject
    end if

    set theText to "--" & theSubject & return & "message:%3c" & message id of theMessage & "%3e" & return & content of theMessage
    tell application "OmniFocus"
        tell default document
            parse tasks with transport text theText as single task singleTask
        end tell
    end tell
end using terms from
end process_message
4

2 回答 2

0

当您“移动”邮件项目时,您可能要求邮件服务移动它们,这需要“同步”才能执行该操作。您的脚本需要等到该操作完成,然后继续执行。

您的标记方法可能是最有效的;标记是在本地数据库上完成的,而移动只能在连接可用时发生,并且在规定的时间间隔内发生。

于 2014-01-15T15:05:21.590 回答
0

我希望这会有所帮助,Message 对象的状态为已删除,您必须检查这一点。如果您从邮箱中移动邮件,它会不断重新出现并导致重复。

repeat with aMessage in messages
  if deleted status of aMessage is false then
    move message to theArchiveFolder
  end if
end repeat

Mail.app 有一个 ui 菜单项,用于删除邮箱中已删除的所有邮件:“Erase deleted items...”

于 2014-09-13T20:27:28.237 回答