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

 
Applescript to Strip Off Leading Characters Thread Tools Search this Thread Display Modes
I have set my OF mail rule to automatically create an OF action whenever an email arrives from selected addresses with a subject line that begins with "@OF", and it works very well for me. I am using this approach to create tasks when working from the office. My question is this: Is there any easy way to have OF automatically strip off the "@OF " as it imports new actions from the mail rule? I understand that Applescript might be the way to go, but being new to both OmniFocus and Applescript, need help in this.
 
You could probably hack the MailAction.applescript inside the OmniFocus app bundle to do this. But I'm not sure that's the best course of action.

The '@' symbol in particular is what OmniFocus uses to find a context in the message, so that if you sent an email "Pick up cat food @errands" it would create an action "Pick up cat food" and set its context to "errands".

Here's some more detail:
http://www.macsparky.com/blog/2010/1...th-e-mail.html
 
I just coded up two different approaches to this. You can pick which you like better.

Approach #1 is to modify the MailAction handler to strip off the "@OF " prefix from the subject line of the message. You can configure the prefix to be stripped by editing subjectPrefix in the script, just be sure to add whatever whitespace you are going to use in your messages to delimit the prefix. Note that this will strip the prefix even if you configure OmniFocus/Mail to use the "+omnifocus" method, and in that case, if your email subject line started with "@context" and "@context" matched your subjectPrefix, it would be stripped away before being processed by OmniFocus, which would lose you the context assignment, as Lizard mentioned. I think most people will naturally write the task name first, followed by any context, project, date, etc. specifications, so this probably isn't an issue.

What is an issue is that you'll have to apply this change to every new version of OmniFocus you install. That might be pretty annoying if you are using the sneaky peek builds. You may also need to update it if Omni changes the MailAction handler. It doesn't happen very often (they apparently can't even be bothered to put in the one line change I gave them to help users debug why their messages aren't arriving in OmniFocus, http://forums.omnigroup.com/showthre...5091#post95091), but you should be aware of the possibility. That said, here's the code:

Code:
-- Copyright 2007 The Omni Group.  All rights reserved.
--
-- $Header: svn+ssh://source.omnigroup.com/Source/svn/Omni/branches/OmniFocus/1.9.x/OmniGroup/Applications/OmniFocus/App/Preferences/MailAction.applescript 110059 2009-03-12 04:33:13Z kc $

property subjectPrefix : "@OF "
property prefixLength : count of subjectPrefix

using terms from application "Mail"
	-- 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)
		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
		
		set theSubject to subject of theMessage
		if (theSubject begins with subjectPrefix) then
			tell application "OmniFocus"
				set newSubject to rich text (1 + prefixLength) thru -1 of theSubject
			end tell
			set theSubject to newSubject
		end if
		set singleTask to false
		if (theSubject starts with "Fwd: ") then
			-- Whole forwarded messages shouldn't split.
			set singleTask to true
			set theSubject to text 6 through -1 of theSubject
		end if
		set theText to theSubject & return & content of theMessage
		tell application "OmniFocus"
			tell default document
				parse tasks with transport text theText as single task singleTask
			end tell
		end tell
	end process_message
	
	on perform mail action with messages theMessages
		try
			set theMessageCount to count of theMessages
			repeat with theMessageIndex from 1 to theMessageCount
				my process_message(item theMessageIndex of theMessages)
			end repeat
		on error m number n
			tell application "OmniFocus"
				log "Exception in Mail action: (" & n & ") " & m
			end tell
		end try
	end perform mail action with messages
end using terms from
Another approach, much easier to maintain, but a little more work to use, is to have a script that simply strips off the subject prefix from any actions in the selection. No worries about interference with the processing, or Omni changing the code out from under you. It's just a slightly modified version of a script Curt Clifton wrote to strip suffixes off of projects and actions another script had used to tag them for attention.

Code:
(*
	This script takes the currently selected actions or projects and simply strips the prefix added by the user.
	
	Based on Clear "Missing NA" Suffixes script by Curt Clifton
	
	version 0.1, by Curt Clifton
	
	Copyright © 2007, Curtis Clifton
	
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	
		• Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
		
		• Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
		
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	version 0.5.2: Unchanged
	version 0.1.1: Fixed bug that was exposed by OmniFocus 1.1
	version 0.1: Original release
*)

property prefixToStrip : "@OF "
property prefixLength : count of prefixToStrip

tell application "OmniFocus"
	tell front document
		tell document window 1
			set theSelectedItems to value of selected trees of content
			repeat with anItem in theSelectedItems
				tell anItem
					if (name begins with prefixToStrip) then
						set newName to rich text (1 + prefixLength) thru -1 of (get name)
						set name to newName
					end if
				end tell
			end repeat
		end tell
	end tell
end tell
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change leading? davidn OmniFocus 1 for Mac 3 2007-12-18 05:04 AM
baseline and leading willb OmniGraffle General 0 2007-06-15 08:18 AM
Strip formatting from pasted text? Roger Barre OmniOutliner 3 for Mac 1 2007-04-02 02:24 PM
Confused about leading... Orlando OmniGraffle General 3 2006-07-24 06:02 AM


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


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