我有一個AppleScript,執行以下操作。爲什麼AppleScript會在文件夾移動後繼續查找郵件消息?
- 激活Mail.app。
- 查找未標記的特定文件夾中的消息(在我的用法中爲「OmniFocus」)。
- 運行這些消息的腳本,將它們添加爲Omnifocus中的任務。
- 標記每條消息。
- 將每條消息移至歸檔文件夾。
我添加了標記步驟(步驟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