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 Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Copy last sent message to Clippings Thread Tools Search this Thread Display Modes
Hi all,

I've discovered the fantastic applescript at http://dltj.org/article/copy-last-se...e-as-rtf-link/ which copies your last sent message as a symbolic link to the clipboard...

I'm sure there's an easy way to then tell Omnifocus to add an action that takes the subject line of your last email, appending it with something like 'Awaiting response', and to paste the symbolic link of the message to your clippings note.

My applescript knowledge is next to non-existent. Anyone know how?
__________________
Plan to be spontaneous

Last edited by smarsh; 2011-03-13 at 12:38 PM..
 
Anybody got any flashes of inspiration?
__________________
Plan to be spontaneous
 
To my understanding, isn't possible to manipulate RTF text from within Applescript, so actually stuffing the snazzy link created by that script into the note field of a new action is difficult. I futzed around a bit trying to get the System UI scripting stuff to simulate pasting the link from the clipboard into the Quick Entry box, but decided it wasn't worth the trouble.

However, it's not difficult at all to put the anchor text of the URL on one line and the actual link on the next, so I did that. I also enhanced the script a bit so you can choose by message subject instead of having to know which account might have been used to send it. You can select whether the script stuffs the action directly in the Inbox, or puts it in the Quick Entry box where you can edit before saving. All the parameters are in the first few lines of the script if you want different behavior.

Code:
-- Various bits of the email pulled out for use in creating new action
global _sub
global _msglnk
global _anchorText
property pNoMessageAvailable : "No message available"

property pUseQuickEntry : true (* if true, Quick Entry window used and left open
                              if false, actions added directly to Inbox *)
property pChooseAccounts : false (* if true, choose by account, otherwise by message *)
property pActionSuffix : " (Awaiting response)" (* string added to subject to create action name *)

on run
	tell application "OmniFocus"
		if (my GetLastSentMailMessage()) then
			my GenerateAction()
		end if
	end tell
end run

on GenerateAction()
	-- tell OmniFocus to make a new action in Quick Entry window or Inbox
	-- populate new action with note containing descriptive text followed by URL on next line
	
	set TheNote to _anchorText & "
" & _msglnk
	set TheAction to _sub & pActionSuffix
	tell application "OmniFocus"
		if (pUseQuickEntry) then
			tell quick entry
				open
				set NewTask to make new inbox task with properties {name:TheAction, note:TheNote}
			end tell
			activate
		else
			tell front document
				set NewTask to make new inbox task with properties {name:TheAction, note:TheNote}
			end tell
		end if
	end tell
end GenerateAction

-- GetLastSentMailMessage
-- based on "Copy last sent message as RTF link" by Disruptive Library Technology Jester
-- http://dltj.org/article/copy-last-sent-message-as-rtf-link/

on GetLastSentMailMessage()
	
	tell application "Mail"
		-- Ask the user which account to use
		set _accts to get accounts
		set _Choices to {}
		set _Accounts to {}
		repeat with eachAccount in _accts
			-- Only offer the enabled accounts for the user to choose
			if enabled of eachAccount then
				set the end of _Accounts to name of eachAccount as string
				if (pChooseAccounts) then
					set the end of _Choices to name of eachAccount as string
				else
					try
						set _msg to first message of mailbox "Sent" of account (name of eachAccount as string)
						set _sub to _msg's subject
					on error
						set _sub to pNoMessageAvailable
					end try
					--	set the end of _Choices to subject of first message of mailbox "Sent" of account eachAccount
					set the end of _Choices to _sub
				end if
			end if
		end repeat
		
		if (pChooseAccounts) then
			set titleStr to "Select Account"
			set promptStr to "Select the account from which to copy a link of the last sent message..."
		else
			set titleStr to "Select Message"
			set promptStr to "Select the last sent message to copy as a link..."
		end if
		set _selection to (choose from list _Choices with title titleStr with prompt promptStr default items (item 1 of _Choices))
		if (_selection is false) then return false -- user pressed Cancel button
		
		if (pChooseAccounts) then
			set _selectedAccount to _selection
		else
			-- need to figure out which account corresponded to message chosen
			if first item of _selection is pNoMessageAvailable then
				return false
			end if
			set _selectedAccount to false
			
			repeat with _i from 1 to count of _Choices
				if first item of _selection is item _i of _Choices then
					set _selectedAccount to item _i of _Accounts
					exit repeat
				end if
			end repeat
			
		end if
		
		if _selectedAccount is false then
			return false -- shouldn't be possible, but...
		end if
		
		set _selectedAccountName to _selectedAccount as string
		
		try
			-- Get the "last" message of the Sent mailbox of the selected account
			set _msg to first message of mailbox "Sent" of account _selectedAccountName
		on error
			return false -- probably didn't have any messages
		end try
		
		-- Get various properties of the message
		set _date to _msg's date sent
		
		try
			set _recipient to name of first recipient of _msg
			set _test to _recipient
		on error
			-- if the Recipient's name property was blank, use the e-mail address instead
			set _recipient to address of first recipient of _msg
		end try
		
		set _sub to _msg's subject
		if _sub starts with "Re:" then
			-- Remove the "Re:" prefix from messages
			set _sub to text 5 through (length of _sub) of _sub
		end if
		
		set _msgid to _msg's message id
		
		-- Create the URL to the message
		set _msglnk to "message://%3C" & my urlencode(_msgid) & "%3E"
		set _anchorText to "Message sent " & _date & " to " & _recipient & " regarding '" & _sub & "'"
		
	end tell
	return true
end GetLastSentMailMessage

on urlencode(theText)
	do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of theText
end urlencode
Attached Files
File Type: zip Save last sent email as OF action.zip (10.8 KB, 170 views)
 
Thanks @whpalmer4, works really well, especially when integrated with fastscripts for quick access.

Cheers
__________________
Plan to be spontaneous
 
Thanks for publishing the modifications! I've updated the original post with a link to this thread.
 
...quick improvement for those following along at home:

change...

Code:
try
	set _recipient to name of first recipient of _msg
	set _test to _recipient
on error
	-- if the Recipient's name property was blank, use the e-mail address instead
	set _recipient to address of first recipient of _msg
end try
to

Code:
try
	set _recipient to name of first item of to recipients of _msg
	if _recipient is missing value then set _recipient to address of first item of to recipients of _msg
on error
	-- if the Recipient's name property was blank, use the e-mail address instead
	set _recipient to address of first item of to recipients of _msg
end try
...for better recipient handling in 10.8.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add to clippings Arnie OmniOutliner 3 for Mac 4 2010-02-09 04:07 AM
Request ability to copy projects, copy actions, add group fernelius OmniFocus for iPhone 0 2009-10-17 06:22 PM
What's your clippings shortcut? jjb OmniFocus 1 for Mac 9 2009-07-07 10:50 PM
Clippings matthew.e OmniFocus 1 for Mac 2 2009-04-16 04:29 PM
all gone. server copy is blank. local copy is blank. lyallmarcus OmniFocus 1 for Mac 10 2009-01-14 03:06 PM


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


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