2013-05-20 44 views
0

我有一個AppleScript,執行以下操作。爲什麼AppleScript會在文件夾移動後繼續查找郵件消息?

  1. 激活Mail.app。
  2. 查找未標記的特定文件夾中的消息(在我的用法中爲「OmniFocus」)。
  3. 運行這些消息的腳本,將它們添加爲Omnifocus中的任務。
  4. 標記每條消息。
  5. 將每條消息移至歸檔文件夾。

我添加了標記步驟(步驟4),因爲沒有它,即使腳本已經移動到存檔文件夾,腳本仍然重複地找到相同的消息。這導致了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 

回答

0

當您「移動」的郵件項目,你可能問的郵件服務來打動他們,這需要以執行該動作的「同步」。您的腳本需要等到該操作完成後才能繼續。

您的標記方法可能是最有效的;標記是在本地數據庫上完成的,而移動只能在連接可用時以及按照規定的時間間隔進行。

0

我希望這有助於消息對象具有刪除狀態,你必須檢查這一點。如果您從郵箱移動郵件,郵件會不斷重新顯示並導致重複。

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

Mail.app具有UI菜單項刪除在一個郵箱中刪除的所有郵件:「抹掉已刪除項目......」

相關問題