Jump to content
sterling

Sending mail to Omnifocus

Recommended Posts

I'm using the AppleScript below as part of a workflow to get mail messages to OmniFocus. This recently broke. I'm just wondering if anyone has a clue what the issue may be? Not sure if this is due to an Apple update, or an OmniFocus one.

 

Error message

[2017-01-11 10:29:46][input.keyword] Processing output of 'action.applescript' with arg ''
[2017-01-11 10:29:46][ERROR: action.applescript] {
    NSAppleScriptErrorAppName = OmniFocus;
    NSAppleScriptErrorBriefMessage = "Unable to convert the string \U201cmessage://<03D12B465157E52C189B6D836173733D254AF6F9@AMX>\U201d to a url. Make sure that the string is a valid URL, including quoting characters that aren't valid in URLs.";
    NSAppleScriptErrorMessage = "OmniFocus got an error: Unable to convert the string \U201cmessage://<03D12B465157E52C189B6D836173733D254AF6F9@AMX>\U201d to a url. Make sure that the string is a valid URL, including quoting characters that aren't valid in URLs.";
    NSAppleScriptErrorNumber = 1;
    NSAppleScriptErrorRange = "NSRange: {1161, 94}";
}
[2017-01-11 10:29:46][action.applescript] Processing output of 'output.notification' with arg ''

applescript

on alfred_script(q)
(*
OmniFocus Selected Mail Messages.applescript
	Copyright (c) 2015 Jonathan 'Wolf' Rentzsch: http://rentzsch.com
	Some rights reserved: http://opensource.org/licenses/mit
  
Pure AppleScript reimplementation of OmniGroup's Clip-o-Tron for OmniFocus.

Hopefully this implementation will be more resilient against OS X, Mail, and OmniFocus updates.
Successfully tested against OS X 10.10.1, OmniFocus 1.10.6, and OmniFocus 2.0.4.

References:
<http://daringfireball.net/2007/12/message_urls_leopard_mail>
<http://forums.omnigroup.com/showthread.php?t=30754>
<http://shawnblanc.net/box/smarter_clip-o-tron.txt>
 *)

tell application "Mail"
	set _sel to the selection
	repeat with _msg in _sel
		set _msgSubject to _msg's subject
		set _msgBody to _msg's content
		set _msgSender to _msg's sender
		set _msgURL to "message://<" & _msg's message id & ">"
		tell application "OmniFocus"
			tell quick entry
				set _task to make new inbox task with properties {name:_msgSubject, note:_msgBody}
				tell note of _task
					insert "From: " & _msgSender & "  Original Message" & (ASCII character 10) & (ASCII character 10) at before paragraphs
					set value of attribute "link" of style of characters -1 thru -17 of first paragraph to _msgURL
				end tell
			end tell
		end tell
	end repeat
end tell

tell application "OmniFocus"
	tell quick entry to open
end tell
tell application "System Events" to keystroke tab
end alfred_script

 

Share this post


Link to post

I use the same script in Mail Act-On and it's working fine for me.

 

Looks like a URL encoding problem from the error messages....is this happening with every message?

 

Usually message:// URLs will have the full domain at the end e.g. @mail.gmail.com and the like but your example only has @AMX which would lead me to think there's something with that particular message that's breaking the script.

 

 

Share this post


Link to post

@sterling Did you ever find a solution? If so, could you post it here?

 

Like yourself, I'd also like to send messages from Mac's Mail app to a new Omnifocus task. At the moment, I use a pretty mundane apple script in Alfred that simply kicks off the Clip-o-Tron from Mail's menu (included below for others that might want something simple). However, I always find myself having to change the titles or wanting to add a context, and I was hoping to tailor something to my needs, like your script above.

 

Thanks for any help you can lend!

 

on alfred_script(q)
if application "Mail" is running then
tell application "Mail" to activate
	delay 0.2
tell application "System Events"
 	   tell process "Mail"
     	   click menu item "OmniFocus 2: Send to Inbox" of menu 1 of menu item "Services" of menu 1 of menu bar item "Mail" of menu bar 1
	end tell
end tell
else
	display notification "To send a message from Mail to OmniFocus, Mail must be open" with title "AI - Not Open"
end if
end alfred_script

 

Share this post


Link to post

I came across this thread while searching for a solution to send mails to the OmniFocus inbox after an update to macOS High Sierra (there the service menu doesn't show up anymore in the context menu; see Omnifocus Clip-o-Tron). So, first of all, thanks for sharing the script.

 

While comparing the resulting URL from the native OmniFocus Service with the one appearing in the error message in the post of @sterling, I noticed the missing encoding of "<" and ">". So, replacing the line

set _msgURL to "message://<" & _msg's message id & ">"

with

set _msgURL to "message:%3C" & _msg's message id & "%3E"

solved the problem for me.

 

I'm not sure if this is a valid solution, since the workflow did work and just broke at some point in time. Additionally I would prefer to do the encoding with a tool designed specifically for that task, because just replacing some characters will not catch all cases. But I didn't found a simple solution for this right away.

 

Hopefully this helps and maybe someone can give a hint if I am on the right track?

Share this post


Link to post
on alfred_script(q)
	tell application "Mail"
		set lst to (get selection)
		set _msgRecent to my GetNewerMessage(lst)
		repeat with _msg in _msgRecent
			set _msgSubject to _msg's subject
			set _msgBody to _msg's content
			set _msgSender to _msg's sender
			set _msgURL to "message://%3c" & _msg's message id & "%3e" -- Fixed: OmniFocus doesn't accept "<" and ">" as part of a URL
			tell application "OmniFocus"
				tell quick entry
					set _task to make new inbox task with properties {name:_msgSubject, note:_msgBody}
					tell note of _task
						insert "From: " & _msgSender & "  Original Message" & (ASCII character 10) & (ASCII character 10) at before paragraphs
						set value of attribute "link" of style of characters -1 thru -17 of first paragraph to _msgURL
					end tell
				end tell
			end tell
		end repeat
	end tell
	
	tell application "OmniFocus"
		tell quick entry to open
	end tell
	tell application "System Events"
		tell process "OmniFocus"
			key code 125
		end tell
	end tell
	
end alfred_script

on GetNewerMessage(lst)
	if lst = {} then return
	if (count lst) = 1 then return (item 1 of lst)
	set msg to (item 1 of lst)
	repeat with i from 1 to (count lst) - 1
		using terms from application "Mail"
			if CompareDates(date received of msg, date received of (item (i + 1) of lst)) then
				set msg to (item i of lst)
			else
				set msg to (item (i + 1) of lst)
			end if
		end using terms from
	end repeat
	return msg
end GetNewerMessage

on CompareDates(A, B)
	if A > B then
		return true
	else
		return false
	end if
end CompareDates

This script is what I am using right now.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×