我有一个执行以下操作的 AppleScript。
- 激活 Mail.app。
- 在未标记的特定文件夹中查找消息(在我的用法中为“OmniFocus”)。
- 对这些消息运行脚本以将它们添加为 Omnifocus 中的任务。
- 标记每条消息。
- 将每封邮件移动到存档文件夹。
我添加了标记步骤(步骤 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