The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Recursion (http://forums.omnigroup.com/showthread.php?t=21770)

akelley 2011-08-01 07:16 AM

Recursion
 
1 Attachment(s)
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

RobTrew 2011-08-01 09:32 AM

[QUOTE=akelley;100153]I would appreciate any help on being able to traverse tasks within action groups. [/QUOTE]

In addition to its [I]tasks[/I] collection, every task also has a [I]flattened tasks[/I] 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[/CODE]

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[/CODE]

akelley 2011-08-01 04:40 PM

Groups & Single Tasks
 
1 Attachment(s)
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

RobTrew 2011-08-01 09:57 PM

[QUOTE=akelley;100179]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. [/QUOTE]

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[/CODE]

RobTrew 2011-08-01 10:53 PM

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
[/CODE]

[COLOR="White"]--[/COLOR]


All times are GMT -8. The time now is 07:28 AM.

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