How to get all flagged tasks using JXA

I use the following Apple script to do the same thing, which works perfectly.

tell application "OmniFocus"
	tell default document
		set flaggedTasks to every flattened task whose flagged is true and completed is false
		
		repeat with a from 1 to length of flaggedTasks
			
			set currentTask to item a of flaggedTasks
			
			set taskName to name of currentTask
			set taskID to id of currentTask

			
		end repeat
	end tell
end tell

But the following JXA doesn’t work at all, it just returns to nothing. Just don’t understand what’s wrong with this JXA.

var app = Application.currentApplication();
app.includeStandardAdditions = true;

var omniFocus = Application("OmniFocus");
var defaultDocument = omniFocus.defaultDocument;

var flaggedTasks = defaultDocument.flattenedTasks.whose({flagged: true, completed: false});

for (var i = 0; i < flaggedTasks.length; i++) {
  var currentTask = flaggedTasks[i];
  var taskName = currentTask.name();
  var taskID = currentTask.id();
}

What happens if you using a pair of trailing parentheses ? Like:

const flaggedTasks = defaultDocument.flattenedTasks.whose({flagged: true, completed: false})()

Unfortunately, I do not have the machine in front of me to test.

1 Like

Thank you for the reply!
Unfortunately, it still does not work. This is really strange, I suppose there should be nothing wrong explicitly.

Thank you for the screenshot. Try this, in order to see a list of OmniFocus objects which satisfy those conditions:

(() => {
	"use strict"
	const
		appOF = Application("OmniFocus"),
		doc = appOF.defaultDocument,
		flaggedTasks = doc.flattenedTasks.whose({flagged: true, completed: false});
		
	return flaggedTasks()
})()
1 Like

Thank you so much! btw, where should I run this script, in the Script Editor or in OmniFocus Automation?

I ran it in Script Editor and here is the result:

If i want to get all flagged tasks using OmniAutomation rather than JXA in an external script, what should I do next, because I’m not very familiar with OmniAutomation API and I guess that can be kind of different from JXA.

flattenedTasks.filter(x => x.flagged)

You could run this line of code in OmniAutomation console.

Do note that calling .flagged property on a Task object returns a Boolean value

1 Like

As you found out, JXA scripts are executed in Script Editor (or with osascript shell command).

1 Like

Thank you so much! And could you please give me some further instructions on how to get all tasks with a certain tag (like ‘morning’, ‘afternoon’, or ‘evening’), and how to get all tasks with the due date of today?
(Just because I’m building a little plugin to send all the tasks which have a tag of ‘morning’, ‘afternoon’ or ‘evening’, is due today, or is flagged to Habitica, I have finished other parts, including making HTTP requests, but I’m just not very familiar with Omni Automation API)
Actually, after making several attempts, I’ve found out how to do with the tag, I know Tag.byIndentifier(${identifier}).tasks should work, however, I just wonder if there exist other ways, for example, to get the tags by name, so that I can get the tasks with all the tags I would like more easily.

Out of my house now, but you can look at tagNamed instance function of Tag class (and also Database class).

1 Like