The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus 1 for Mac (http://forums.omnigroup.com/forumdisplay.php?f=38)
-   -   Is there a way to invoke send to OF service from an applescript? (http://forums.omnigroup.com/showthread.php?t=21882)

gadgetdoctor 2011-08-15 07:21 AM

Is there a way to invoke send to OF service from an applescript?
 
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?

RobTrew 2011-08-15 10:01 PM

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:
[URL="http://daringfireball.net/2007/12/message_urls_leopard_mail"]http://daringfireball.net/2007/12/message_urls_leopard_mail[/URL]

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:

[SIZE="2"][I][SIZE="1"]message:<159433453FAD4620AA3C5F19528697B6@DC8YB82J>[/SIZE][/I][/SIZE]


[CODE]on run
tell application id "emal"
-- GET THE FIRST ITEM SELECTED IN MAIL
set lstSeln to (selection as list)
if (count of lstSeln) < 1 then return

-- BUILD AN HTML VERSION OF A LINK TO THE MESSAGE
set strHTML to my WrapHTML(my GetMsgLink(first item of lstSeln), "Original Message")
end tell

-- PLACE AN RTF VERSION OF THE MESSAGE LINK IN THE CLIPBOARD
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

[/CODE]

RobTrew 2011-08-17 01:43 AM

[QUOTE=RobTrew;100619]I haven't found a satisfactory way to place RTF in an OmniFocus note through Applescript[/QUOTE]

Incidentally, if the clipboard contains formatted text, you can get the corresponding RTF code in a compressed format,

[CODE]set datRTF to «class RTF » of (the clipboard as record)[/CODE]

or as uncompressed and human-legible RTF code

[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)))'"[/CODE]

and either of these can, of course, be written to a file.

[COLOR="White"]--[/COLOR]

gadgetdoctor 2011-08-17 01:48 AM

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.

gadgetdoctor 2011-08-17 11:22 PM

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"[/CODE]

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
[/CODE]


All times are GMT -8. The time now is 11:32 PM.

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