(* Move Actions and Send to Omni Copyright © 2011 BrianOfLondon V 002 update May 18 2015 V 003 update July 12 2015 - added my filter system Scan all IMAP mail folders for a folder called "flag-omni" if found take all messages in the folder, - send them to OmniFocus - flag them - copy them back to the inbox - delete them from flag-omni Some bits from this script from Daring Fireball: http://daringfireball.net/2008/11/flagging_messages_from_iphone_mail *) set my_special_mailbox to "[flag-omni]" display notification "Checking the [flag-omni] mailboxes" using terms from application "Mail" tell application "Mail" set _imap_accts to every imap account set _count to 0 repeat with _acct in _imap_accts try set _flagbox to mailbox my_special_mailbox of _acct set _flagbox_count to count messages of _flagbox set _count to _count + _flagbox_count if _flagbox_count > 0 then try set theMessageCount to count of (messages of _flagbox) repeat with theMessageIndex from 1 to theMessageCount -- check for the blue flag if flag index of (item theMessageIndex of (messages of _flagbox)) is 4 then -- Do nothing set _count to _count - 1 display notification "Flagged already, skipping \n" & subject of item theMessageIndex of messages of _flagbox else my process_message(item theMessageIndex of (messages of _flagbox)) end if end repeat on error m number n tell application "OmniFocus" log "Exception in Mail action: (" & n & ") " & m end tell end try set flagged status of every message of _flagbox to true set flag index of every message of _flagbox to 4 -- set background color of every message of _flagbox to blue -- Since Maveriks the Move doesn't work to take messages -- out of the [flat-omni] box anymore -- Don't move messages, the flag will stop them from causing issues. Perhaps institute a thing to move them after a certain number of days. -- move every message of _flagbox to mailbox "INBOX" of _acct end if end try end repeat if _count is 0 then display notification "Nothing Moved\nNo messages found" else if _count is 1 then set _msg_string to " message." else set _msg_string to " messages." end if display notification "Messages Moved\nFlagged and moved " & _count & _msg_string end if end tell -- Return the message ID in a way that OmniFocus will turn into a link -- It doesn't matter if the message is then moved back to the inbox, -- the link still works. on GetMsgLink(oMsg) return "message:///%3C" & message id of oMsg & "%3E" end GetMsgLink -- Copyright 2007 The Omni Group. All rights reserved. -- -- $Header: svn+ssh://source.omnigroup.com/Source/svn/Omni/tags/OmniFocus/1.9.2/GM-v77.75.9/OmniGroup/Applications/OmniFocus/App/Preferences/MailAction.applescript 110059 2009-03-12 04:33:13Z kc $ -- Trims "foo " down to "foo@bar.com" on trim_address(theAddress) try set AppleScript's text item delimiters to "<" set WithoutPrefix to item 2 of theAddress's text items set AppleScript's text item delimiters to ">" set MyResult to item 1 of WithoutPrefix's text items on error set MyResult to theAddress end try set AppleScript's text item delimiters to {""} --> restore delimiters to default value return MyResult end trim_address on process_message(theMessage) tell application "OmniFocus" log "OmniFocus calling process_message in MailAction script" end tell -- Don't want to filter based on the sender! -- Allow the user to type in the full sender address in case our trimming logic doesn't handle the address they are using. -- set theSender to sender of theMessage -- set trimmedSender to my trim_address(theSender) -- -- tell application "OmniFocus" -- set AllowedSender to allowed mail senders -- if AllowedSender does not contain trimmedSender and AllowedSender does not contain theSender then -- return -- end if -- end tell -- end of changes 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 -- This section added 18 Aug 2011 Gadgetdoctor to include link -- to the original email in the Omnifocus note set theLink to "Original Message: " & my GetMsgLink(theMessage) -- Now run special rules set theSubject to run_my_rules(theMessage, theSubject) set theText to theSubject & return & theLink & return & (content of theMessage as rich text) -- end of changes display notification theText tell application "OmniFocus" tell default document parse tasks into with transport text theText as single task singleTask end tell end tell end process_message on run_my_rules(theMessage, theSubject) -- Specific rules for me if ("A Subject you want to find" is in the subject of theMessage) or ("another subject" is in the subject of theMessage) then set thisProject to "Some Project Name" set thisContext to "A Context" set thisDueBy to "tomorrow" end if -- Now process and return the new subject including OmniFocus codes set theSubject to theSubject & " ::" & thisProject & " @" & thisContext & " #" & thisDueBy return theSubject end run_my_rules end using terms from