The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus 1 for Mac
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Is there a way to invoke send to OF service from an applescript? Thread Tools Search this Thread Display Modes
I'm not a very advanced coder but i'm mashing something up at the moment. I have a basic script that does send a message to OmniFocus's inbox and uses code that I re-cycled from the applescript that's used by the mail rule.

However that script doesn't include a hyperlink to the original email like the link I get if I manually hit my keyboard shortcut in mail and invoke the Service Omnifocus: Send to Inbox.

My question: is there an easy way to run that service from inside an applescript if one has a particular mail message already identified in the script?
Launching services from Applescript is not something that I've tried, but you should be able to build some kind of a link back to the original message in one of the formats listed by John Gruber in:

This code snippet below, from which you may be able to salvage one or two relevant elements, creates an RTF link to the selected Mail message and places the link in your clipboard. I haven't found a satisfactory way to place RTF in an OmniFocus note through Applescript, but you could usefully place a plain text version of the link, on the pattern of something like:

message:<159433453FAD4620AA3C5F19528697B6@DC8YB82J >

on run
	tell application id "emal"
		set lstSeln to (selection as list)
		if (count of lstSeln) < 1 then return
		set strHTML to my WrapHTML(my GetMsgLink(first item of lstSeln), "Original Message")
	end tell
	do shell script "echo " & quoted form of strHTML & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
end run

on GetMsgLink(oMsg)
	using terms from application "Mail"
		"message:%3c" & message id of oMsg & "%3e"
	end using terms from
end GetMsgLink

on WrapHTML(strLink, strTitle)
	"<a href=\"" & strLink & "\">" & strTitle & "</a>"
end WrapHTML

Last edited by RobTrew; 2011-08-15 at 11:01 PM..
Originally Posted by RobTrew View Post
I haven't found a satisfactory way to place RTF in an OmniFocus note through Applescript
Incidentally, if the clipboard contains formatted text, you can get the corresponding RTF code in a compressed format,

set datRTF to «class RTF » of (the clipboard as record)
or as uncompressed and human-legible RTF code

set strRTF to do shell script "osascript -e 'the clipboard as «class RTF »' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))'"
and either of these can, of course, be written to a file.

I'm working on this exactly now... there seems to be an "insert" method for the RTF note of an omnifocus text blog. I'm working to see if what is on the clipboard can be inserted this way.
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:

"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.

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:

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

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

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
				set _flagbox to mailbox my_special_mailbox of _acct
				set _count to _count + (count messages of _flagbox)
					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")
			if _count is 1 then
				set _msg_string to " message."
				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:// 110059 2009-03-12 04:33:13Z kc $
	-- Trims "foo <>" down to ""
	on trim_address(theAddress)
			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
		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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Omnifocus: Send to Inbox" Service is Missing chipwarren OmniFocus 1 for Mac 1 2012-07-02 08:13 PM
How to remove "Send To Inbox" service amdi8 OmniFocus 1 for Mac 3 2011-11-25 12:51 AM
Invoke Quick Entry by using Applescript Rogier OmniFocus Extras 4 2009-11-25 12:27 AM
Service menu's Send to Inbox gives an Error with OmniOutliner adam.sindelar OmniOutliner 3 for Mac 0 2009-05-12 12:55 AM
"OmniFocus: Send to Inbox" service grayed out Kevin Yank OmniFocus 1 for Mac 2 2007-11-19 02:37 PM

All times are GMT -8. The time now is 10:09 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.