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

 
Recursion Thread Tools Search this Thread Display Modes
I would appreciate any help on being able to traverse tasks within action groups. The assumption here is that the user has selected the action groups 'Group 1' & 'Group 2' and wants the script to perform some action on the tasks contained within them.

Thanks
Alex
Attached Thumbnails
Click image for larger version

Name:	OF Example.jpg
Views:	380
Size:	12.0 KB
ID:	1983  
 
Quote:
Originally Posted by akelley View Post
I would appreciate any help on being able to traverse tasks within action groups.
In addition to its tasks collection, every task also has a flattened tasks collection. (A flat list of every task in its sub-tree).

i.e. if you don't need to preserve information about depth and nesting, you can bypass recursion and just use the flattened tasks collection.

Code:
-- LOOP THROUGH FLATTENED SUB-TREEs OF SELECTED GROUPs (DROPPING INFORMATION ABOUT NESTING AND DEPTH)
tell application id "OFOC"
	tell front document
		tell content of front document window
			set lstGroup to value of selected trees where class of value is task or class of value is inbox task
			set str to ""
			repeat with oGroup in lstGroup
				set str to str & return & name of oGroup & ":" & return
				repeat with oTask in flattened tasks of oGroup
					set str to str & tab & "- " & name of oTask & return
				end repeat
			end repeat
		end tell
	end tell
end tell
return str
If, on the other hand you do need to use the nesting and depth information, recursion is easily achieved.

Code:
-- PROCESS SUB-TREES RECURSIVELY, PRESERVE INFORMATION ABOUT NESTING
tell application id "OFOC"
	tell front document
		tell content of front document window
			set lstGroup to value of selected trees where class of value is task or class of value is inbox task
			set str to ""
			repeat with oGroup in lstGroup
				set str to str & my ProcessGroup(oGroup, "")
			end repeat
			return str
		end tell
	end tell
end tell
return str

-- RECURSE THRU SUBTREE OF TASK
on ProcessGroup(oTask, strIndent)
	set str to ""
	using terms from application "OmniFocus"
		-- DO SOMETHING WITH THE CURRENT NODE,
		set str to strIndent & "- " & name of oTask & return
		
		-- AND RECURSE THROUGH ITS DESCENDANTS
		set strIndent to strIndent & tab
		repeat with oSubTask in tasks of oTask
			set str to str & my ProcessGroup(oSubTask, strIndent)
		end repeat
	end using terms from
	return str
end ProcessGroup

Last edited by RobTrew; 2011-08-01 at 10:28 AM..
 
Thanks Rob! I tried it and it works well. I'm using your recursion example in order to preserve hierarchy.

Is it possible to handle selected groups and single actions together at the same time (see example)? I seem to be having trouble determining if an item is an action group or a single task.

Cheers,
-Alex
Attached Thumbnails
Click image for larger version

Name:	OF Example.jpg
Views:	312
Size:	11.8 KB
ID:	1984  
 
Quote:
Originally Posted by akelley View Post
Is it possible to handle selected groups and single actions together at the same time (see example)? I seem to be having trouble determining if an item is an action group or a single task.
The ProcessGroup() function above should already work with single actions.

If you need to distinguish between groups and single tasks you can branch on a condition like:
Code:
if number of tasks of oItem > 0
 
And if you particularly want to pick out childless tasks at the top level (of the inbox or a project), you could test for them with a function like:

Code:
on IsTopLevelSingleTask(oItem)
	using terms from application "OmniFocus"
		tell oItem
			if number of tasks > 0 then return false
			set {oParent, oProject} to {parent task, containing project}
		end tell
		
		if oProject is missing value then -- inbox task
			(oParent is missing value) -- true if no parent task
		else -- project task
			(oParent is root task of oProject) -- true if parent is project
		end if
	end using terms from
end IsTopLevelSingleTask
--

Last edited by RobTrew; 2011-08-02 at 06:27 AM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -8. The time now is 04:02 AM.


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