Get tasks from Perspective using JXA

Hi all, new to AppleScript so I figured I’d have more luck with JXA, but it seems even more inscrutible.
I’m trying to use the REPL to suss out the methods and structure, but I can’t use the normal JavaScript approaches as I don’t get a human-readable representation, just a lot of “can’t convert types” errors. Is there an approach to suss out the methods and attributes of an object in JXA?

Ultimately, I’m trying to make a script that dumps a list of flattened tasks from a perspective in a structured data format (prefereably JSON). I plan to feed this to Alexa so I can have my daily tasks as part of my “flash briefing”.

I am getting stuck on how to flatten and iterate through the contentTree to pull out the individual tasks.

I tried the approach @unlocked2412 suggested in another thread for pulling all the tasks in the database, but it seemed a bit slow and I would rather not duplicate the perspective filters.

Perhaps that is a better approach, since this way changes the perspective in the current window which will be a problem when I’m trying to work and this script runs in the background.

Any thoughts? This is what I have thus far

(function() {
	'use strict';

	const myPerspective = "Hot";

	var of = Application('OmniFocus');
	var ofDoc = of.defaultDocument;

	var win = ofDoc.documentWindows.at(0);
	win.perspectiveName.set(myPerspective);

	var hotContent = win.content();


	// writing out as UTF8 using objectiveC bridge
	var reportStr = $.NSString.alloc.initWithUTF8String('foo');
	reportStr.writeToFileAtomicallyEncodingError('/Users/lambda/alexatest/todo.json', true, $.NSUTF8StringEncoding,     null);

})();

Happy to help! In the Utilities folder on your Mac, you’ll find one named Script Editor; go ahead and drop OmniFocus’ app icon onto Script Editor. This will open up a window which shows the AppleScript commands which are available. (You can do this with any app that offers an AppleScript interface; it’s not OmniFocus-specific.)

The browser defaults to AppleScript, but there should be a “Language” item in the toolbar which will let you switch between AS, JavaScript, and Objective-C formatting.

1 Like

I’d like to do this too, and I can’t find a way to make it work. Is it possible to get a list of tasks in a perspective without changing the UI to make that perspective visible?

Specifically, I’d like to get the first task of a perspective (the same task that appears on my Apple Watch) and show it in my menu bar using BitBar.

Perspectives doesn’t seem to keep track of tasks. Perhaps you could try to get tasks in database that match the conditions in your perspective ?

By what’s in the AppleScript dictionary they don’t seem to, but they functionally do on the watch. My watch face is configured to show me the first task in one of my perspectives (which I have configured to be the task I should probably be working on right now). I’d like to get show that task on my menu bar on my Mac too.

Since the perspective rules aren’t exposed via scripting either, I don’t think there’s a way to do that without completely reimplementing the perspective in Apple/JavaScript, and then keeping it up to date if I ever change the perspective, which sounds pretty terrible.

Of course, it’s not the ideal solution. It only makes sense with a perspective that gets changed very rarely.