View Single Post
Here's the script as I currently have it:

Code:
-- Copyright 2007 The Omni Group.  All rights reserved.
--
-- $Header: svn+ssh://source.omnigroup.com/Source/svn/Omni/branches/OmniFocus/1.x/OmniGroup/Applications/Focus/App/Preferences/MailAction.applescript 107989 2009-01-23 22:26:17Z xmas $

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 theAttachments to mail attachments of the theMessage
		set theAttachmentCount to count of theAttachments
		
		set theSubject to subject of theMessage
		set singleTask to false
		if (theSubject starts with "FW: ") then
			-- Whole forwarded messages shouldn't split.
			set singleTask to true
			set theSubject to text 5 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
		
		set theTaskCount to the count of theTasks
		if theTaskCount > 0 and theAttachmentCount > 0 then
			set theTask to the first item in theTasks
			
			repeat with theAttachmentIndex from 1 to theAttachmentCount
				set theAttachment to item theAttachmentIndex of theAttachments
				set theAttachmentFileName to name of theAttachment
				set theFullAttachmentPath to "Kajind:tmp:" & theAttachmentFileName
				
				save theAttachment in theFullAttachmentPath
				
				try
					tell front document of application "OmniFocus"
						tell the note of theTask
							make new file attachment with properties {file name:theFullAttachmentPath, embedded:true}
						end tell
					end tell
				on error errmesg number errn
					write_error_log(errmesg & return & return & "error number: " & (errn as text))
				end try
				tell application "Finder"
					if exists file theFullAttachmentPath then
						delete file theFullAttachmentPath
					end if
				end tell
			end repeat
		end if
		
	end process_message
	
	try
		-- put up a growl notification
		tell application "GrowlHelperApp"
			set the allNotificationsList to {"Mail Notification"}
			set the enabledNotificationsList to {"Mail Notification"}
			register as application "OmniFocus" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "OmniFocus"
			notify with name "Mail Notification" title "OmniFocus recieved Mail task" description theSubject application name "OmniFocus"
		end tell
	end try
	
	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