Counting tasks that have no context

I have the following:

  • [Document]
    • [Folder] Decide
      • [Project] Personal Decisions
      • [Project] Business Decisions
        • [Task (with no context)] Test

I have an Applescript that contains the following as a condition for calling a subroutine:

-- Decisions
set nDecisions to count ((every task of flattened contexts) where ((name of containing project's folder = "Decide") and (blocked is false)))
if nDecisions > 0 then
my conditions("Decide")
return
end if

Problem: If the task “Test” does not have a context assigned (i.e., “No Context”), it does not get counted. Yet as soon as I assign a context to it, it gets counted.

Why does my Applescript not count tasks without contexts, and how do I change it so it does?

You are asking for every task of flattened contexts. That is asking the contexts for their tasks. So it doesn’t include any tasks which have no context.

Perhaps something like this?

tell application "OmniFocus"
	tell default document
		set projectList to flattened projects of folder named "Testing Debris"
		repeat with decideProject in projectList
			set nDecisions to number of available tasks
			if nDecisions > 0 
				my conditions("Decide")
				return
			end if
		end repeat
	end tell
end tell

I don’t remember the nuances of “available” versus “blocked”, so you may want to put that logic back in…

1 Like

THANK YOU!! Works with slight modifications (tell blocks omitted):

set projectList to flattened projects of folder named “Decide”
            repeat with decideProject in projectList
                set nDecisions to number of available tasks in decideProject
                if nDecisions > 0 then
                    my conditions(“Decide”)
                    return
                end if
            end repeat


To provide context for future forum participants with similar problems, this relates to a modification of the excellent “SmartPerspective” script of @brandon (with the help of @ediventurin), available here: https://github.com/brandonpittman/OmniFocus/blob/master/SmartPerspective.applescript.

(I was having trouble getting OmniFocus to count tasks with no context, and this seemed to solve the problem.)

1 Like

Oh yeah, you wouldn’t need the tell blocks in a larger script that already had that info elsewhere. I just had them to test the script locally.

Yes, thanks. I needed to change the folder name (obviously) but also changed “set nDecisions to number of available tasks” to “set nDecisions to number of available tasks in decideProject”.

It’s working great!

By the way, to me it would seem useful to have an Applescript class (terminology?) that includes all tasks, regardless of whether they have a context or not: for example, I have to script “every available task in flattened contexts”, and that doesn’t include tasks with “no context”. Why can’t I just say “every available task”, and get a count of all available tasks, whether or not they have a context assigned?

I’m definitely no scripting guru, and I’m sure there is something I don’t know/appreciate/understand about this. But this suggestion seems like an improvement that could be made to OmniFocus’s Applescript library in the future.

1 Like

I agree with @apkawel on this: Though I like having the ability to be really specific in what I’m scripting, sometimes it would be helpful to have simple, straightforward terms for jobs like these. (AppleScript is, after all, supposed to be an “English-like” language that allows people with little coding background to construct useful tools for their apps.)

To me, the great success of OmniFocus 2 is that it’s created a usable, elegant interface for all those the powerful capabilities developed in OF1. In the same spirit, maybe @kcase, @lizard, and crew could add a “Automation Audit” to the roadmap with an eye on boosting usability by simplifying the grammar and nomenclature?

3 Likes

Agreed with both @apkawel and @Veritrope. “Automation Audit” is a great idea!

2 Likes