Thread: Recursion
View Single Post
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..