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

 
Detecting childless projects and tasks in Applescript Thread Tools Search this Thread Display Modes
Using where / whose clauses is generally quite a lot faster (and simpler to write) in Applescript than iterating with repeat loops and applying tests.

For efficient recursive walks through nested outlines, one really wants to be able to distinguish, within a where statement, between those nodes which have children and those which are childless leaves.

This is not as easy in OmniFocus as it is in OmniOutliner, in which the Applescript library provides the useful has subtopics property.

Has anyone found an elegant or efficient idiom for doing something similar in OmniFocus ?

A rather clumsy, and rather inefficient-looking idiom which does at least work is:

Code:
where (container of (tasks of tasks) is tasks)
which filters for childless (taskless) projects or tasks, so a rather unreadable equivalent of has subtopics is

Code:
where not (container of (tasks of tasks) is tasks)
To see a list of projects which have no tasks, for example, you could write:

Code:
tell application "OmniFocus"
	set oDoc to default document
	set lstChildless to my ChildlessProjects(oDoc)
	set focus of front document window of oDoc to lstChildless
end tell

on ChildlessProjects(oParent)
	using terms from application "OmniFocus"
		tell oParent
			set lstProjects to projects where (container of (tasks of tasks) is tasks) and ((status is active) or (singleton action holder is true))
			set lstFolders to folders where hidden is false
		end tell
		repeat with oFolder in lstFolders
			set lstProjects to lstProjects & my ChildlessProjects(oFolder)
		end repeat
		return lstProjects
	end using terms from
end ChildlessProjects
But I suspect that something more efficient and legible should be possible ...

Any thoughts ?

--

Last edited by RobTrew; 2010-06-20 at 03:13 PM..
 
I haven't come up with anything better. I played with things like count of tasks of task is 0, but without success. I wasn't very persistant in my efforts though, since I have recursions that do the job reasonably well.

It would be really helpful if there were a command to get all the tasks in a tree as a flat list filtered by where clauses. Most of the gyrations in scripting OF seem to have to do with manually walking the trees.
__________________
Cheers,

Curt
 
I've added some tiny scripting updates in OmniFocus 1.8 (r134083) that may help make these sorts of tasks easier.

Document and folder now have a "flattened projects" relationship which returns an in-order traversal of the projects within the specified container.
Document now has a "flattened tasks" relationship which returns an in-order traversal of the entire task tree in the document (including the inbox).

Tasks now have three additional properties:
  • number of tasks
  • number of available tasks
  • number of completed tasks

which reflect internal counters that we are maintaining anyway.

For this job, you might be able to do something like:

Code:
tell application "OmniFocus"
	tell default document
		every flattened project whose number of available tasks is 0 and status is active
	end tell
end tell
I hope this helps!
__________________
CTO, The Omni Group
 
Quote:
Originally Posted by Tim Wood View Post
Document and folder now have a "flattened projects" relationship which returns an in-order traversal of the projects within the specified container.
Document now has a "flattened tasks" relationship which returns an in-order traversal of the entire task tree in the document (including the inbox).

Tasks now have three additional properties:
  • number of tasks
  • number of available tasks
  • number of completed tasks
Wow ! Words which sound like the falling of welcome rain :-)

I really appreciate the time and thought which you have put into doing that work ...

Thank you, Tim !
 
PS as a very short term fix, for anyone holding on the Sneaky Peek builds, it turns out that non-leaf nodes were findable in a marginally less clumsy fashion than I had previously stumbled on with:

Code:
tasks where (tasks of tasks) is not tasks
and terminal leaves were harvestable with:

Code:
tasks where (tasks of tasks) is tasks
Happily, due to the excellent work which Tim has modestly announced, these slightly baroque formulations are now, at best, of rather obscure antiquarian interest :-)
 
Quote:
Originally Posted by RobTrew View Post
these ... formulations are now, at best, of rather obscure antiquarian interest :-)
Although, (unless Tim happens to feel that there is scope for bringing contexts up to date with projects ?) this kind of idiom would, at present, still be needed if we wanted to use whose / where clauses to flatten the OF Context tree.

To get a flattened list of active contexts for example, you could currently write:

Code:
tell application "OmniFocus"
	set oDoc to default document
	set lstContexts to my ActiveContexts(oDoc)
end tell

on ActiveContexts(oParent)
	using terms from application "OmniFocus"
		tell oParent
			set lstContexts to contexts where (hidden is false) and (allows next action is true)
			set lstParental to contexts where (contexts of contexts) is not contexts
			repeat with oContext in lstParental
				set lstContexts to lstContexts & my ActiveContexts(oContext)
			end repeat
		end tell
		lstContexts
	end using terms from
end ActiveContexts
This could be very drastically shortened, however, (and the sequence of the harvest made more rational), with the excellent approach that Tim has brought to the folder, project and task tree ...

--

Last edited by RobTrew; 2010-06-21 at 03:20 AM..
 
Quote:
Originally Posted by Tim Wood View Post
I hope this helps!
Woot! Very, very nice. Thanks.
__________________
Cheers,

Curt
 
"flattened context" is available on document and context as of r134086. I've also added "flattened task" on task.

Thanks for the feedback!
__________________
CTO, The Omni Group
 
The flattened lists are already working well, and very welcome.

Not sure if others are seeing this, but there seems to be an initial glitch with the very promising family of three new number of ... tasks properties.

I am getting AppleEvent handler failed messages from the following, in Late Night Software's Script Debugger (and the script produces no output if launched from the OF script menu).

(Sneaky Peeks 1.8 Revision: 134152)

Code:
tell application "OmniFocus"
	set oProject to first item of flattened projects of default document
	tell oProject
		set lngAvailable to number of available tasks
		set lngCompleted to number of completed tasks
		set lngTasks to number of tasks
		
		display dialog ((lngAvailable as string) & tab & lngCompleted as string) & tab & lngTasks as string
	end tell
end tell
--

Last edited by RobTrew; 2010-06-22 at 04:27 AM..
 
Quote:
Originally Posted by RobTrew View Post
Not sure if others are seeing this, but there seems to be an initial glitch with the very promising family of three new number of ... tasks properties.
--
( This turned out to only affect OS X 10.5 and earlier, and has been fixed for sneaky peek 1.8 r134216 )
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
repeating projects/tasks when all tasks/subtasks are complete? djc225 OmniFocus 1 for Mac 1 2012-03-20 06:55 PM
Applescript Framework to Loop Through Projects and Tasks elrjax OmniFocus Extras 2 2012-01-23 11:04 PM
Applescript for dependent projects fudster OmniFocus Extras 10 2011-03-17 03:00 PM
AppleScript for deleting completed tasks and projects snarke OmniFocus Extras 8 2009-01-20 02:22 PM
Q: Tasks (or projects) waiting on other tasks? budney Applying OmniFocus 10 2008-12-30 04:42 AM


All times are GMT -8. The time now is 01:33 PM.


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