View Single Post
Well I didn't get exactly what I wanted... but I've got something that is better than I had and certainly good enough.

I have achieved the basic point I wanted: a clickable link back to the original email from the OmniFocus document. Turns out it was as simple as adding in a single line in the following format:

Code:
"message:%3C" & message id of oMsg & "%3E"
OmniFocus is kind enough to make that clickable.

The main point is that in any of my IMAP accounts I can move (on iPhone for e.g.) a mail to a folder called [omni-flag] and the next time I run this code it will be swept up, placed in OmniFocus and returned to the Inbox (with a coloured flag)

For anyone else, here's my messy, hacked together from parts on the web, not very elegant code. YMMV.

Code:
(*
Move Actiona and Send to Omni
Copyright © 2011 GadgetDoctor

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

*)
global myNotificationsList
set my_special_mailbox to "[flag-omni]"

set myNotificationsList to {"Checking the [flag-omni] mailboxes", "Messages Moved", "Nothing Moved"}

setup_growl()
notify_growl("Checking the [flag-omni] mailboxes", "In Progress")


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 _count to _count + (count messages of _flagbox)
				try
					set theMessageCount to count of (messages of _flagbox)
					repeat with theMessageIndex from 1 to theMessageCount
						my process_message(item theMessageIndex of (messages of _flagbox))
					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
				move every message of _flagbox to mailbox "INBOX" of _acct
			end try
		end repeat
		if _count is 0 then
			my notify_growl("Nothing Moved", "No messages found")
		else
			if _count is 1 then
				set _msg_string to " message."
			else
				set _msg_string to " messages."
			end if
			my notify_growl("Messages Moved", "Flagged and moved " & _count & _msg_string)
			-- display alert "Flagged 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 <foo@bar.com>" 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
		-- 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)
		
		-- Don't want to filter based on the sender!
		--
		--		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)
		set theText to theSubject & return & theLink & return & (content of theMessage as rich text)
		-- end of changes
		
		tell application "OmniFocus"
			tell default document
				parse tasks with transport text theText as single task singleTask
			end tell
		end tell
	end process_message
	
	
	
end using terms from

on setup_growl()
	tell application "System Events"
		set isRunning to ¬
			(count of (every process whose name is "GrowlHelperApp")) > 0
	end tell
	
	if isRunning then
		tell application "GrowlHelperApp"
			-- ******* These lines are needed to register this script with Growl
			-- They are not needed every time it's run
			
			-- Make a list of all the notification types 
			-- that this script will ever send:
			set the allNotificationsList to contents of myNotificationsList
			
			-- Make a list of the notifications 
			-- that will be enabled by default.      
			-- Those not enabled by default can be enabled later 
			-- in the 'Applications' tab of the growl prefpane.
			set the enabledNotificationsList to contents of myNotificationsList
			
			-- Register our script with growl.
			-- You can optionally (as here) set a default icon 
			-- for this script's notifications.
			register as application ¬
				"SAP to Omnifocus" all notifications allNotificationsList ¬
				default notifications enabledNotificationsList ¬
				icon of application "OmniFocus"
		end tell
	end if
end setup_growl


on notify_growl(n, d)
	
	tell application "System Events"
		set isRunning to ¬
			(count of (every process whose name is "GrowlHelperApp")) > 0
	end tell
	
	set nClass to class of n
	if nClass is integer then
		set n to item n of myNotificationsList
	else if myNotificationsList contains n then
		
	else
		set n to 5th item of myNotificationsList
	end if
	
	
	if isRunning then
		tell application "GrowlHelperApp"
			notify with name n title n description d application name "SAP to Omnifocus"
		end tell
	end if
	
	
end notify_growl