Unflag Available Tasks?

I’ve used Shortcuts a ton to automate omnifocus stuff for a long time, but have a desire that would only be able to be done by Omni Automation.

I don’t know how to script. Does anyone or could anyone put together an OmniFocus Automation that unflags all available tasks? (Or all available tasks that do or don’t have certain tags?)

I feel comfortable with editing an existing automation and trial and error. I’d like to use this in my morning review. Unflag everything to help me be deliberate about what I choose to flag for the day.

FWIW, this should get you started:

(() => {
	// main :: ()
	const main = () => {
	  const
	    availableTasks = flattenedTasks.
	      filter(x => Task.Status.Available === x.taskStatus);
	      
	   console.log(
	      `The number of available tasks is ${
	        availableTasks.length
	      }`  
	   )
	};

	return Application('OmniFocus')
        .evaluateJavascript(
            `(${main})()`
        );
})();

availableTasks is bound to an array of Task objects. Each of those, has a flagged property. Hence, a specific item flagged property could be changed with the following assignment:

availableTasks[0].flagged = false

To apply that assignment across the whole array, perhaps:

availableTasks.forEach(x => x.flagged = false)

It gives me an error when I try to run it and I don’t how anything about scripting to know how to proceed. I appreciate the effort though

TypeError: Application is not a function. (In ‘Application(‘OmniFocus’)’, ‘Application’ is an instance of CallbackObject) undefined:15:20

Sure. You should paste that code in, for example, Script Editor. And, set the language tab to JavaScript. This script should run in macOS. Of course, omniJS is a cross-platfrom API, so there should be a way of creating something similar for iOS/iPadOS.

I see that you ran the code in the Console. That’s why it gave the error.

(() => {
	const
	    availableTasks = flattenedTasks.
	      filter(x => Task.Status.Available === x.taskStatus);
	      
	   console.log(
	      `The number of available tasks is ${
	        availableTasks.length
	      }`  
	   )
})();

You can certainly run this version in the OmniFocus Automation Console.

Thank you for that, but pasting that into the console still gives me an error saying something is undefined.

Someone did point me toward this script. I tried messing with it for a while but I know nothing about any scripting language and could not figure out how to modify this script to only address ‘available’ tasks rather than ‘uncompleted’ ones. If you or anyone here would be able to do that to this script, that’d be helpful. If not, I at least feel I made an attempt to find an answer to this idea. Shortcuts can’t really modify an OF database, just query for data.

Here’s that script: https://github.com/ferlatte/omnifocus-plugins/blob/main/clear-flag-from-tasks.omnijs

Checked again, and the script works. Did you paste the whole code in the Console. We should be able to sort this out.

Thank you. When I run that in the console, I get two lines returned:

“The number of available tasks is 235”
and
“< undefined”

Good. The script works.

(() => {
	const
		availableTasks = flattenedTasks.
			filter(x => Task.Status.Available === x.taskStatus);
		
	// availableTasks.forEach(x => x.flagged = false)  
		console.log(
			`The number of available tasks is ${
				availableTasks.length
			}`  
		)
})();

I’ve commented the line that updates flagged property of the whole collection of tasks. This version could be run in the Console.

This gives me the same result, giving me the number of available tasks and saying ‘undefined.’

I don’t know what things mean in order to fiddle with it. Do you mean to say this script should replace something within the first one you posted?

I just meant that if you “uncomment” that line, i.e. remove the leading forward slashes…

availableTasks.forEach(x => x.flagged = false)

the script should accomplish what you want.

console.log is the last statement executed and it returns the value undefined. A more informative return value could be displayed, I think.

Hey, it works! Thanks so much.

1 Like