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

 
AppleScript that adds Waiting For emails from Mail.app (delegated tasks) to OmniFocus Thread Tools Search this Thread Display Modes
All,

I've developed a small script that uses MailTags and an Mail.app outbox rule to add a task to the OmniFocus Inbox for every task you delegate by email and tag accordingly before sending. It automatically assigned the right context to the tasks, offers some level of customisation and uses Growl to display notifications once the task has been added to OF.

Check here for description, instructions and download. I hope you enjoy the script and find it as useful as me.

Cheers,

simplicitybliss
 
I cannot get this script working. It simply does not create the task. Any help would be appreciated.
 
Quote:
Originally Posted by menza View Post
I cannot get this script working. It simply does not create the task. Any help would be appreciated.
Is your waiting context nested by chance? I could not get it to work either if my waiting for context was a sub-context. Once I changed it to a top level context, the script works fine.

As a question to simplicitybliss, I noticed that you include a link to the original email in the OmniFocus task. That link does not work for me-takes me to Mail but the sent message is not selected. It seems logical that the link would not work, as I assume the outgoing message is not written to the 'Sent' items folder until after the outgoing Mail rule is executed, but I'm curious why you included the link, or does the link actually work for you?
 
Greg,

I modified the script so that link back to the original email now works. Please check the link to the original blog post to re-download the script from there. Thanks.

With regards to the issue with nested "waiting for contexts", I haven't have a solution yet, but I can confirm that in this case no task is created and the script stays quiet.

If anyone can help on assigning tasks to a nested context using AppleScript it would be highly appreciated.

Cheers,

simplicitybliss
 
Link back now works great-thanks!
 
I have been able to get this to work and after some googling I have found several places where it is stated that Rules on outgoing will not work automatically since you have apply Rules manually on specifically the folder Sent (I have this stored on our Exchange server if it makes any difference). How do you guys make the Rules work automatically on outgoing mails?
 
I want to thank Sven Fechner, Scott Morrison, whpalmer4, jgrafix, papalpha, and Greg Jones for the scripts and guidance that has helped me with this. I use this script daily, and find it invaluable. I recently decided to start using more start dates and very few due dates in OF...so utilized the great scripts and advice from you to cobble something together. This script will produce a start date only (no due date) which is determined by entering the tickler date in MailTags. It also works if you don't enter anything in the tickler field...leaving blank start and due fields. I know this isn't that big a deal...but I'm horrible at AppleScript...so this took me awhile to figure out. I figured I'd post it for anybody interested.

Code:
(*
	Waiting For Mails to OmniFocus Script
	by simplicityisbliss.com, Sven Fechner
	MailTags project and due date compatibility added by Scott Morrison, Indev Software
	
	Based on an Outbox Rule (Mail Act-On) in Mail.app this script adds specific messages
	to the OmniFocus Inbox with the waiting for context
	
	MailTags is required to automatically set the project and the due date.

	Mail Act-On (www.indev.ca) is required to define the Outbox Rule to only
	create tasks for those outgoing emails that are to be tracked in OmniFocus
	
	A sample Outbox rule may be
	if MailTags Tickle Date is After 0 days today  
	Run Apple Script: [ThisAppleScript]
	
				
	The script uses Growl for feedback notification if it is installed and running
	
*)


--!! EDIT THE PROPERTIES BELOW TO MEET YOUR NEEDS !!--

-- Do you want the actualy mail body to be added to the notes section of the OmniFocus task?
-- Set to 'true' is or 'false' if no
property mailBody : true

-- Text between mail recipient (the person you are waiting for to come back) and the email subject
property MidFix : "to follow up re:"

-- Name of your Waiting For context in OmniFocus
property myWFContext : "Waiting for"

-- Default due time
property timeStart : "11:59:00 PM"

-- !! STOP EDITING HERE IF NOT FAMILAR WITH APPLESCRIPT !! --

--Configuration for Growl messages 
property GrowlRun : false
property scriptName : "Waiting For Mails to OmniFocus"
property notifySuccess : "Success Notification"
property notifyFail : "Failed Notification"
property titleSuccess : "Waiting For Mail added"
property titleFail : "Waiting For Mail to OmniFocus FAILED"
property txtSuccess : " added to OmniFocus successfully"
property txtFail : " to OmniFocus to add successfully"
property txtIcon : "OmniFocus"

on perform_mail_action(theData)
	
	--Check if Growl is running
	tell application "System Events" to set GrowlRun to (count of (every process whose name is "Growl")) > 0
	
	--Setup Growl
	if GrowlRun then tell application "Growl" to register as application scriptName all notifications {notifySuccess, notifyFail} default notifications {notifySuccess, notifyFail} icon of application txtIcon
	
	--Get going
	tell application "Mail"
		set theMessages to |SelectedMessages| of theData --Extract the messages from the rule
		repeat with theMessage in theMessages
			set theSubject to subject of theMessage
			set theRecipient to name of to recipient of theMessage
			set theMessageID to urlencode(the message id of theMessage) of me
			
			set theStartDate to date "Tuesday, January 1, 1991 11:59:00 PM"
			try
				using terms from application "MailTagsScriptingSupport"
					set theStartDate to (due date of theMessage) as date
					set theStartDate to my setDate(theDueDate)
				end using terms from
			on error theError
				
			end try
			
			--	tell application "Growl" to notify with name notifySuccess title titleSuccess description "test" & txtSuccess application name scriptName
			--	do shell script "echo test"
			
			
			-- Check if there is one or more recipients
			try
				if (count of theRecipient) > 1 then
					set theRecipientName to (item 1 of theRecipient & (ASCII character 202) & "and" & (ASCII character 202) & ((count of theRecipient) - 1) as string) & (ASCII character 202) & "more"
				else
					set theRecipientName to item 1 of theRecipient
				end if
				
				set theTaskTitel to theRecipientName & (ASCII character 202) & MidFix & (ASCII character 202) & theSubject
				set messageURL to "Created from message://%3C" & (theMessageID) & "%3E"
				set theBody to messageURL
				if mailBody then set theBody to theBody & return & return & the content of theMessage
				
				-- Add waiting for context task to OmniFocus
				tell application "OmniFocus"
					tell default document
						set theContext to context myWFContext
						--	set theTask to make new inbox task with properties {name:theTaskTitel, note:theBody, context:theContext, due date:theDueDate}
						
						if theStartDate is not date "Tuesday, January 1, 1991 11:59:00 PM" then
							set theTask to make new inbox task with properties {name:theTaskTitel, note:theBody, context:theContext, start date:theStartDate}
						else
							set theTask to make new inbox task with properties {name:theTaskTitel, note:theBody, context:theContext}
							
						end if
						if myProject is not null then
							set theProject to project myProject
							move theTask to end of tasks of theProject
							
						end if
					end tell
				end tell
				
			on error theError
				do shell script "logger -t outboxrule 'Error : " & theError & "' "
			end try
			
			my GrowlSuccess("Mail: " & theSubject)
		end repeat
	end tell
	
end perform_mail_action

on GrowlSuccess(theMessage)
	if GrowlRun then tell application "Growl" to notify with name notifySuccess title titleSuccess description theMessage & txtSuccess application name scriptName
end GrowlSuccess

on urlencode(theText)
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode

on setDate(theStartDate)
	set theDate to (date string of theStartDate)
	set newDate to the (date (theDate & " " & timeStart))
	-- tell application "Growl" to notify with name notifySuccess title "test"
	return newDate
end setDate
The only thing missing is the ability to dictate my own time...my scripting seems to have killed that functionality and I can't figure out how to get it back. As it is...I keep getting my Start dates at 12:00 AM. If anybody wanted to help me out with this...I'd be very appreciative.

Cheers.
 
Quote:
Originally Posted by bpwhistler View Post
The only thing missing is the ability to dictate my own time...my scripting seems to have killed that functionality and I can't figure out how to get it back. As it is...I keep getting my Start dates at 12:00 AM. If anybody wanted to help me out with this...I'd be very appreciative.

Cheers.
Here is the modified script that I use. It has start and due dates as well as start and due times. The interval between start and due days is user-defined also. You could pull the set time code out and add it to your script.

Code:
(*
	Waiting For Mails to OmniFocus Script
	by simplicityisbliss.com, Sven Fechner
	MailTags project and due date compatibility added by Scott Morrison, Indev Software
	
	Based on an Outbox Rule (Mail Act-On) in Mail.app this script adds specific messages
	to the OmniFocus Inbox with the waiting for context
	
	MailTags is required to automatically set the project and the due date.

	Mail Act-On (www.indev.ca) is required to define the Outbox Rule to only
	create tasks for those outgoing emails that are to be tracked in OmniFocus
	
	A sample Outbox rule may be
	if MailTags Tickle Date is After 0 days today  
	Run Apple Script: [ThisAppleScript]
	
				
	The script uses Growl from the App Store for feedback notification if it is installed and running
	
*)


--!! EDIT THE PROPERTIES BELOW TO MEET YOUR NEEDS !!--

-- Do you want the actualy mail body to be added to the notes section of the OmniFocus task?
-- Set to 'true' is or 'false' if no
property mailBody : true

-- Text between mail recipient (the person you are waiting for to come back) and the email subject
property MidFix : "to follow up re:"

-- Name of your Waiting For context in OmniFocus
property myWFContext : "waiting"

-- Default start time
property timeStart : "5:00:00 AM"

-- Default due time
property timeDue : "6:00:00 PM"

-- Default start to due date interval, in days
property dateInterval : "3"

-- !! STOP EDITING HERE IF NOT FAMILAR WITH APPLESCRIPT !! --

--Configuration for Growl messages 
property GrowlRun : true
property scriptName : "Waiting For Mails to OmniFocus"
property notifySuccess : "Success Notification"
property notifyFail : "Failed Notification"
property titleSuccess : "Waiting For Mail added"
property titleFail : "Waiting For Mail to OmniFocus FAILED"
property txtSuccess : " added to OmniFocus successfully"
property txtFail : " to OmniFocus to add successfully"
property txtIcon : "OmniFocus"

on perform_mail_action(theData)
	--Check if Growl is running
	tell application "System Events" to set GrowlRun to (count of (every process whose name is "Growl")) > 0
	
	--Setup Growl
	if GrowlRun then tell application "Growl" to register as application scriptName all notifications {notifySuccess, notifyFail} default notifications {notifySuccess, notifyFail} icon of application txtIcon
	
	--Get going
	tell application "Mail"
		set theMessages to |SelectedMessages| of theData --Extract the messages from the rule
		repeat with theMessage in theMessages
			set theSubject to subject of theMessage
			set theRecipient to name of to recipient of theMessage
			set theMessageID to urlencode(the message id of theMessage) of me
			set theStartDate to ""
			set theDueDate to ""
			
			try
				using terms from application "MailTagsScriptingSupport"
					set theStartDate to («class mtdd» of theMessage) as date
					set theStartDate to my setStartDate(theStartDate)
					set theDueDate to theStartDate + dateInterval * days
					set theDueDate to my setDueDate(theDueDate)
				end using terms from
			on error theError
				
			end try
			
			-- Check if there is one or more recipients
			try
				if (count of theRecipient) > 1 then
					set theRecipientName to (item 1 of theRecipient & (ASCII character 202) & "and" & (ASCII character 202) & ((count of theRecipient) - 1) as string) & (ASCII character 202) & "more"
				else
					set theRecipientName to item 1 of theRecipient
				end if
				
				set theTaskTitle to theRecipientName & (ASCII character 202) & MidFix & (ASCII character 202) & theSubject
				set messageURL to "Created from message://%3C" & (theMessageID) & "%3E"
				set theBody to messageURL
				if mailBody then set theBody to theBody & return & return & the content of theMessage
				
				-- Add waiting for context task to OmniFocus
				tell application "OmniFocus"
					tell default document
						set theContext to context myWFContext
						
						if theStartDate is not "" then
							set theTask to make new inbox task with properties {name:theTaskTitle, note:theBody, context:theContext, start date:theStartDate, due date:theDueDate}
						else
							set theTask to make new inbox task with properties {name:theTaskTitle, note:theBody, context:theContext}
							
						end if
						if myProject is not null then
							set theProject to project myProject
							move theTask to end of tasks of theProject
							
						end if
					end tell
				end tell
				
			on error theError
				do shell script "logger -t outboxrule 'Error : " & theError & "' "
			end try
			
			my GrowlSuccess("Mail: " & theSubject)
		end repeat
	end tell
	
end perform_mail_action

on GrowlSuccess(theMessage)
	if GrowlRun then tell application "Growl" to notify with name notifySuccess title titleSuccess description theMessage & txtSuccess application name scriptName
end GrowlSuccess

on urlencode(theText)
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode

on setStartDate(theStartDate)
	set theDate to (date string of theStartDate)
	set newDate to the (date (theDate & " " & timeStart))
	return newDate
end setStartDate

on setDueDate(theDueDate)
	set theDate to (date string of theDueDate)
	set newDate to the (date (theDate & " " & timeDue))
	return newDate
end setDueDate
 
Thanks Greg...I ended up using the set time code as you suggested. I also decided to use 3 iterations of the script: 1 for start dates only, 1 for start/due dates (similar to yours), and 1 for due dates only. I'll use @WaitS, @WaitSD, & @WaitD in Mail Tags to trigger them respectively. Thanks for all the help...Brett
 
I recently upgraded to the App store version of OF. These scripts aren't working anymore! Is this a sandboxing issue?

Thanks,

Brett
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to deal with delegated or "waiting for" actions bisimpson Applying OmniFocus 20 2013-02-27 06:52 AM
Delegated tasks? mattw OmniFocus 1 for Mac 35 2011-10-07 02:10 PM
Sync with Omnifocus on Mac adds completed tasks.. ohla313 OmniFocus for iPhone 7 2011-06-13 08:50 AM
Script to add delegated tasks from entourage to "Waiting" context in OmniFocus tarun101 OmniFocus Extras 1 2010-05-21 08:09 AM
Adding Tasks Via Send To OmniFocus Mail Rule, Mailtags, & Mail Act-On reuben OmniFocus 1 for Mac 0 2007-11-18 06:07 PM


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


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