Automating DT with JavaScript: Making whose work with type

This is a followup to an older threat that got derailed somewhat.

To summarize: AppleScript and JavaScript offer a whose method to filter lists (arrays in JS) by properties. This could be very helpful, if it actually worked as expected in JS (that’s what the other thread was about).

If anyone cares about the background (@cgrunenberg ?), there’s a thread over at OmniFocus:

After much searching and fiddling around, I found this solution to use enumerations (like DT’s type and tagType in the record class) in whose calls. Maybe it helps someone else at some other time…

(() => {
	const app = Application("DEVONthink 3");
	const db = app.databases["DB"];
//  find all PDF documents in the current database 
	const pdfs = db.contents.whose({_match: [ObjectSpecifier().type, "PDF document"]});
	console.log(pdfs.length);
})()

The key part is to use ObjectSpecifier(). in front of the enumerations name. Apple apparently botched JXA enough to not do that internally (but generating the apparently correct event. Well, as some people would say, JXA is a heap of manure).

:stuck_out_tongue_winking_eye:

I’m still looking for the gems in all that :wink:

1 Like