Script to send messages from Mail to OmniFocus

Yesterday I posted about a script to convert messages in Apple Mail to OmniFocus tasks. I used this script for many years, but it stopped working a few months ago (possibly when I changed from Mavericks to Yosemite).

The script contained the command “parse tasks with transport text theText”, which I changed into “parse task into default document with transport text theText”

To test if this solved the problem I executed the modified script with the AppleScript editor Smile and got an error message: “OmniFocus got an error: Some parameter is missing for parse tasks into”.

I drew the conclusion that I had a problem with the ‘parse task into’ command and attempted to solve it in various ways. I did not succeed and turned to this forum to ask for help.

Today (6 October 2015) I discovered that the modified script works fine when run with Apple’s Script Editor, so apparently the problem wasn’t in the script but in Smile. Updating Smile to the most recent version (3.8) solved the problem.

Sorry for bothering you.

In case you are interested, here is the script. To use it: copy the code, paste it into a new Script Editor document and save that document as script in ~/Library/Application Scripts/ Execute it by means of a Mail rule or a script launcher.

(* Add messages to OmniFocus: creates an OmniFocus task from messages in Apple Mail. *)
(* The script can be executed by a mail rule or a script launcher *)
(* The task is created in the OmniFocus project corresponding to the MailTags project of a message. *)
(* In absence of the MailTagsHelper application or a MailTags project the task is created in OmniFocus' inbox. *)
(* The task's title is the subject of the message. *)
(* The task's note contains a link to and the body of the message. *)

using terms from application "Mail"

on convertToOmniFocus(theMessages)
-- the handler to create the task
(* theMessages - a list of refs to messages in Apple Mail *)
	tell application "Mail"
		repeat with thisMessage in theMessages
			set theSubject to subject of thisMessage
			set theContent to content of thisMessage
			set messageURL to get message id of thisMessage
			set messageURL to "<message://%3C" & messageURL & "%3E>"
			set theProject to ""
			if (exists application "MailTagsHelper") then
				tell application "MailTagsHelper" to set theProject to project of thisMessage
				if theProject is not equal to missing value then
					set theProject to ">" & theProject
					set theProject to ""
				end if

			end if
			set theText to theSubject & theProject & return & messageURL & return & return & theContent
			tell application "OmniFocus"
				parse tasks into default document with transport text theText with as single task
			end tell
		end repeat
	end tell
end convertToOmniFocus

on perform mail action with messages theMessages
-- the handler executed by a mail rule
	my convertToOmniFocus(theMessages)
end perform mail action with messages

on run
-- the handler executed by a script launcher
	tell application "Mail"
		set theMessages to selection
		my convertToOmniFocus(theMessages)
	end tell
end run
end using terms from

Apologies if this is right in front of my face, but is the a way to structure e-mails to the OF inbox that set the project/context/date fields as desired? I’ve seen some posts elsewhere about post-processing scripts but as the e-mails are being crafted within python it would be nice to take care of everything there.

Related, is there direct access from python into OF w/out having to go through the kluge e-mail route?

I’m not familiar with Python but I once heard about a utility called ‘PyPI’ that would allow Python scripts to communicate with AppleScriptable applications.

I guess such a bridge can be used to let Python create tasks in OmniFocus using AppleScript. OS X’s osascript command line utility can be used for that purpose too.

Basically, there are two ways to create tasks in OmniFocus with AppleScript:

Method 1: create a string that represents the task (e.g. "task >project @context #3-15-2016 #3-31-2016") and convert that string to a task with the parse task into command (e.g. parse task into default document with transport text "task >project @context #3-15-2016 #3-31-2016")

This is what is done in the above script.

The ‘Advanced Syntax’ section of this document describes the syntax of such a textual representation.

Method 2: tell aProject to make new task with properties {name:"the task", context:theContext, defer date:theDeferDate, due date:theDueDate}

This is explained in David Sparks’ Create OmniFocus tasks with AppleScript.

The parse task into method is easier than the make new task one, because the latter requires you to set up references to projects, contexts and dates in AppleScript.

Hope this helps!