Need to count tasks excluding certain conditions

I have a script to launch perspectives based on certain conditions. At certain point I have the following line, to count available tasks:

		if (count (every available task of every flattened context)) > 0 then

I’d’like to exclude from that count all Errands (context) tasks.


I’d count all tasks, count all tasks in the Errands context, and subtract.

Don’t know how to do that… :(
Could you help?

tell application "OmniFocus"
	set theDoc to default document
	tell theDoc
		set allAvailable to count (every available task of every flattened context)
		set errandsAvailable to count (every available task of context named "errands")
		set actualAvailable to allAvailable - errandsAvailable
	end tell
end tell

@lizard thanks! It works brilliantly. Any chance you can show me how to change it so it takes into account “Errands” and all its sub contexts? :D

If that isn’t feasible, counting all tasks under a folder would do it for me too.

Counting tasks on Errands and its sub-contexts
  • I couldn’t find an easy way. Hints, @omnigroup? :D
Counting tasks under a specific folder
  • I came out with this:
    set nInFolder to count (every available task of every flattened context where name of containing project's folder = "FolderName")

If you don’t have more than two layers of contexts (in other words, Errands > Grocery, but not Errands > Grocery > Circle K), then you could find all the other contexts whose stuff you need to count with

set errandContext to context named "Errands"
set errandChildren to every flattened context whose container is errandContext

You’re actually making the same assumption with your folder solution. It won’t find a project that’s inside a folder that’s inside of “FolderName”

@lizard, thanks!

In fact, I have this one extra level of contexts (Errand > Groceries > Circle K). Only way then would be iterating over Errands and its sub-contexts? Not familiar with that. Trying to find a way.

For now, I’ve set with the Folder based solution (no sub Folders there), but the solution would be much more elegant, and flexible, if I managed to find a(n efficient) way to count the Errands (context) tasks.