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 Today's Posts

 
Tally Completion on a Selected Project Thread Tools Search this Thread Display Modes
I am trying to create a simple script to tally the percentage complete on a selected project, where %complete = completed tasks/total tasks. I must admit that I am floundering on issues of windows, documents, contents, and projects (flattened or not). Here is my test starting point ...

Code:
(*
This script calculates a percentage completion on a selected project
*)

tell application "OmniFocus"
	set oC to the content of front window
	set theSelection to (value of every selected tree of oC)
	if ((count of theSelection) < 1) then
		display alert "You must first select a Project to run this." as warning
		return
	end if
	if ((count of theSelection) > 1) then
		display alert "You currently can only select one Project to run this." as warning
		return
	end if
	(*
	if the type of theSelection is not project then
		display alert "You can only run this on a Project." as warning
		return
	end if
	set theNumber to the (number of tasks in theSelection)
        set theNumberCompleted to the (number of completed tasks in theSelection)
        set thePercentage = theNumberCompleted*100/theNumber
	*)
end tell
The first parts work to parse the list for only one project. How do I extract the project from theSelection in order to parse its number of tasks and completed tasks? I also eventually want this script to run on a project that may be selected in either the left or right pane. What caveats apply?

Past the basics, this script could be used in repeat loops to do a statistical report. My immediate end use is to use the percentage completed on a selected project to adjust the value on a corresponding goal in Goalscape.

Thanks!
 
OK ... this works, in case anyone is interested.

Code:
(*
This script tallies a rough percentage completion on a selected project
It counts based on only top-level "tasks" ... it does not parse through groups
*)

tell application "OmniFocus"
	-- get one selected project
	set oC to the content of front window
	set theSelection to the (selected trees of oC)
	if ((count of theSelection) < 1) then
		display alert "You must first select a Project in the main window." as warning
		return
	end if
	if ((count of theSelection) > 1) then
		display alert "You currently can only select one Project to run this." as warning
		return
	end if
	-- get its name
	set theName to (name of selected trees of oC as rich text)
	-- find it in a list of all projects
	set theList to flattened projects of document of window 1
	repeat with theProject in theList
		if the name of theProject is equal to theName then
			-- get the completion status
			set theCompleted to (number of completed tasks in theProject)
			set theTotal to (number of tasks in theProject)
		end if
	end repeat
	-- return the completion status
	set CP to theCompleted * 100 / theTotal
	set theText to "Completion status of " & theName & " is " & CP & "%"
	display dialog theText
end tell
 
Useful.

FWIW one other approach to getting the selected project is to use a where clause. Sth like this, perhaps:

Code:
property pTitle : "Proportion of tasks completed in selected projects"

tell application id "OFOC"
	tell front document window of default document
		repeat with oPanel in {content, sidebar}
			set lstProj to (value of selected trees of oPanel where class of its value is project)
			set lngProj to length of lstProj
			if lngProj > 0 then exit repeat
		end repeat
		if lngProj < 1 then return
		
		set str to ""
		repeat with oProj in lstProj
			tell oProj
				set {lngTasks, lngDone} to {number of tasks, number of completed tasks}
				set str to str & name & ":   "
				if lngTasks > 0 then
					set str to (str & (((lngDone / lngTasks) * 100) as integer) as string) & "%"
				else
					set str to str & "0%"
				end if
			end tell
			set str to str & return
		end repeat
		display dialog str buttons {"OK"} default button "OK" with title pTitle
	end tell
end tell
--

Last edited by RobTrew; 2012-05-31 at 01:05 AM..
 
I learned much from the overhaul ... many thanks!!!

--
JJW
 
This revision flattens a project to count all tasks, handles empty projects, and outputs in a "table-like" format.

Code:
(*
This script tallies a percentage completion on all projects in rightmost panel window
*)

property pTitle : "Completion Status" -- title of dialog box
property pLine : "-> " -- starting string for a line
property pEmpty : "-0-" -- three character string for empty projects

tell application id "OFOC"
	
	tell front document window of default document
		
		-- collect all content in sidebar
		
		repeat with oPanel in {content, sidebar}
			set lstProj to (value of selected trees of oPanel where class of its value is project)
			set lngProj to length of lstProj
			if lngProj > 0 then exit repeat
		end repeat
		if lngProj < 1 then return
		
		-- generate the completion status list
		
		set str to ""
		
		-- loop through all projects
		
		repeat with oProj in lstProj
			tell oProj
				set strn to " : " & its name
				set lngDone to 0
				-- flatten the task list of the project
				set lstTasks to the (flattened tasks of oProj as list)
				set lngTasks to number of items in lstTasks
				if lngTasks > 0 then
					-- we have tasks in the project
					-- count the completed number of tasks
					repeat with oTask in lstTasks
						if oTask is completed then
							set lngDone to lngDone + 1
						end if
					end repeat
					-- generate the percentage complete
					set pcTask to (((lngDone / lngTasks) * 100) as integer)
					if (pcTask < 10) then
						set strp to pLine & "  " & (pcTask as string) & "%" & tab
					else if (pcTask < 100) then
						set strp to pLine & " " & (pcTask as string) & "%" & tab
					else
						set strp to pLine & (pcTask as string) & "%" & tab
					end if
				else
					-- we have no tasks in the project
					set strp to pLine & pEmpty & tab
				end if
			end tell
			set str to str & strp & strn & return
		end repeat
		display dialog str buttons {"OK"} default button "OK" with title pTitle
	end tell
end tell
One could argue the merits of counting an Action Group as a task in addition to its sub-tasks. That level of refinement is left for those who want to figure out the completion status on a project beyond two significant digits.

A more useful step would be to take this to the point where it can accept a project name passed via an external call from Goalscape, return the % completion status to Goalscape, and have Goalscape set its progress indicator accordingly.

--
JJW
 
 




Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up a recurring project with multiple options for completion waynefritsche Applying OmniFocus 9 2011-08-02 10:07 AM
script to make an inactive project active and move it to a selected folder kingsinger OmniFocus Extras 2 2010-11-15 08:52 PM
New Script - Selected Entourage EMail to Selected OF Task davidsomeone OmniFocus Extras 1 2009-03-06 12:22 PM
Project completion state not being synced to Macs? Richard Flynn OmniFocus Syncing 6 2009-03-03 01:05 AM
Ability to Switch to Project list with Action selected SpiralOcean OmniFocus for iPhone 0 2008-11-20 06:26 AM


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


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