Skip to content

Instantly share code, notes, and snippets.

@amityweb
Last active March 4, 2022 17:11
Show Gist options
  • Save amityweb/5e68042e66d977b2ad81 to your computer and use it in GitHub Desktop.
Save amityweb/5e68042e66d977b2ad81 to your computer and use it in GitHub Desktop.
Send Mail to Omnifocus 2
-- Thanks to https://github.com/HunterHillegas/OmniFocusMailFlags/blob/master/PushFlaggedMessagesToOmniFocus.applescript
-- This AppleScript will send email messages in a specific folder in Mail.app to your Omnifocus 2 inbox
-- Only runs if Mail and OF2 are open
-- Run the script with /usr/bin/osascript "/path/to/script/send-follow-up-to-omnifocus.scpt"
-- I use Lingon to manage my scheduled scripts
-- Edit the variables below
on run
tell application "System Events"
set isOmnifocusRunning to (count of (every process whose bundle identifier is "com.omnigroup.OmniFocus2")) > 0
set isMailRunning to (count of (every process whose bundle identifier is "com.apple.mail")) > 0
end tell
if isOmnifocusRunning and isMailRunning then
-- Edit these variables to suit your own Mail account and folders
set emailAccount to "Email" # Name of your email account in Apple Mail
set emailFolder to "Follow Up" # Name of your email folder in Apple Mail
set archiveAccount to "Email" # Name of your email account in Apple Mail to archive the processed mail to
set archiveFolder to "Archive" # Name of your email folder in Apple Mail to archive the processed mail to
-- End of variables editing
tell application "Mail"
set _msgs_to_capture to (every message of mailbox emailFolder of account emailAccount)
repeat with eachMessage in _msgs_to_capture
if background color of eachMessage is not blue then
set theStart to missing value
set theDue to missing value
set theOmniTask to missing value
set theSubject to (subject of eachMessage)
set theFromName to (extract name from sender of eachMessage)
set theTitle to theSubject & " - " & theFromName
#set theNotes to the content of eachMessage
set theNotes to the content of eachMessage
# Limit num characters, because Omnifocus seems to slow down with large Notes
# We cannot get characters up to 200 if the length is less than 200
# so this will get the characters up to 200
set the theNotesLength to (length of theNotes)
if theNotesLength > 200 then
set theNotes to characters 1 thru 200 of theNotes
else
set theNotes to characters 1 thru theNotesLength of theNotes
end if
set theCombinedBody to "message://%3c" & message id of eachMessage & "%3e" & return & return & theNotes
tell application "OmniFocus"
tell default document
set theContext to context "To Process"
#set fld1 to (first folder whose name is "Work")
#tell fld1
# set theProject to project "General"
#end tell
set newTask to make new inbox task with properties {name:theTitle, note:theCombinedBody, context:theContext, flagged:true}
#set assigned container of newTask to theProject
end tell
end tell
set background color of eachMessage to blue
set read status of eachMessage to true
move eachMessage to mailbox archiveFolder of account archiveAccount
end if
end repeat
end tell
tell application "OmniFocus"
compact front document
end tell
end if
end run
@amityweb
Copy link
Author

Forgot to mention, there's two issues with this script I cannot fix yet:

  1. After the email is moved, there is still an "invisible" copy in the original folder until Mail is closed and its purged. So sometimes the task is created multiple times as it thinks the mail is still in there.

  2. Moving a conversation into the folder will add the task for each mail message. Its best to only move the one mail you want to add as a task by dragging the preview pane, or expanding the threads, and not pulling in the entire conversation. I have not figured out how to only take the first thread in a conversation, and I probably wont bother.

@amityweb
Copy link
Author

amityweb commented Jul 3, 2014

I may have fixed the duplication issue, by setting a message background to blue when it processes a message, then only process a message if its background is not blue, so it seems its not processing messages anymore, I hope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment