View Single Post
Updated version allows customization for use of growl or dialog box, and gracefully handles case where growl not present. Also warns if there are tasks in the selection which do not have duration estimates.

Code:
-- Sum estimated times, display sum in Growl and copy sum to clipboard (for later pasting back into estimated time for an entire project, or elsewhere).
-- Jake Bowers (jwb1970@gmail.com), 2008-24-10
-- largely based on RobTrew's StartTimer.scpt Ver 0.02 for code to adds up durations of selected tasks (http://forums.omnigroup.com/showthread.php?t=4748)
-- and on hint from a11en in thread Minuteur Applescript for Growl Notification. (http://forums.omnigroup.com/showthread.php?t=6749)
-- James O'Leary (jpo@me.com), 2008-08-11 
-- now can handle context view "boxes) (assigned an estimated time of 0) and the time displayed is in hours and minutes instead of
-- just a straight up hours number. Displays in dialog box now instead of copying to clipboard. Item count displayed as well.
-- Aaron Hunyady (aaron@hunyady.net), 2009-08-01
-- Removed "cancel" button in dialog. Total time in minutes is copied to the clipboard.
-- Ben Davidson (benjamin@idavidson.ca), 20009-05-02
-- Updated the Growl notification feature and removed the dialogue box (you need to install the Growl extra, "growlnotify" - included in the install disk image)
-- Changed some of the dialog wording
-- Bill Palmer, 2011-01-30
-- Control use of growl vs. dialog box with useGrowlPref property, gracefully handle case where growlnotify not installed.
-- Also warn if any of the included tasks did not have a duration estimate

property useGrowl : false -- set this to false if you don't (want to) use Growl
property growlNotifyPath : "/usr/local/bin/growlnotify" -- default location for growlnotify installation

tell application "OmniFocus"
	tell front document
		tell document window 1
			set oTrees to selected trees of content
			set lngTrees to count of oTrees
			set underEstimate to ""
			
			if lngTrees > 0 then
				set lngTotal to 0
				repeat with iTree from 1 to lngTrees
					set oTask to value of (item iTree of oTrees)
					
					
					try
						set lngMinutes to estimated minutes of oTask
					on error
						set lngMinutes to 0
					end try
					
					if lngMinutes as string is equal to "missing value" then
						set lngMinutes to 0
						set underEstimate to "
Warning: a task had no estimated duration!"
					end if
					
					
					set lngTotal to lngTotal + lngMinutes
					
				end repeat
			end if
			set the clipboard to lngTotal as rich text
		end tell
	end tell
	set lngHours to 0
	repeat
		if lngTotal < 60 then exit repeat
		set lngTotal to lngTotal - 60
		set lngHours to lngHours + 1
	end repeat
end tell
if lngHours = 1 then
	set strPluralHours to " hour "
else
	set strPluralHours to " hours "
end if
if lngMinutes = 1 then
	set strPluralMinutes to " minute"
else
	set strPluralMinutes to " minutes"
end if
if lngTrees = 1 then
	set strPluralTasks to "task"
else
	set strPluralTasks to "tasks"
end if

if useGrowl then
	set fp to (POSIX file growlNotifyPath)
	tell application "Finder"
		if not (exists fp) then
			set useGrowl to false
		end if
	end tell
end if

if useGrowl then
	do shell script growlNotifyPath & " Estimated Project Time -a OmniFocus -m ' " & lngTrees & " " & strPluralTasks & "
 " & (lngHours) & "" & strPluralHours & "and " & lngTotal & "" & strPluralMinutes & underEstimate & "' "
else
	display dialog "Item count: " & lngTrees & "
" & "Total time: " & (lngHours) & strPluralHours & lngTotal & strPluralMinutes & underEstimate buttons {"OK"} default button 1
end if

Last edited by whpalmer4; 2011-01-30 at 12:05 PM..