为什么AppleScript移动后会继续在文件夹中查找邮件?

时间:2013-05-20 16:08:33

标签: applescript

我有AppleScript执行以下操作。

  1. 激活Mail.app。
  2. 查找未标记的特定文件夹中的邮件(“OmniFocus”,在我的使用中)。
  3. 在这些消息上运行脚本,将其添加为Omnifocus中的任务。
  4. 标记每条消息。
  5. 将每封邮件移至存档文件夹。
  6. 我添加了标记步骤(步骤4),因为没有它,脚本会重复查找相同的消息,即使它们已经移动到Archive文件夹中。这导致OmniFocus中出现了许多重复的任务。该脚本有效,但使用标记状态是一个黑客,我想了解为什么AppleScript在我们的“OmniFocus”文件夹中已经移动到“Archive”文件夹时继续查找消息,以便我可以停止依赖标记状态(并使用该消息所在的文件夹)来确定消息是否已被处理。

    我在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
    

2 个答案:

答案 0 :(得分:0)

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

你的标记方法可能效率最高;标记在本地数据库上完成,而移动只能在连接可用时以规定的时间间隔发生。

答案 1 :(得分: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菜单项,用于删除邮箱中删除的所有邮件:"删除已删除的邮件..."