Quantcast
Channel: DEVONtechnologies Community - Latest posts
Viewing all articles
Browse latest Browse all 17009

Most efficient way to get all nested tags (JXA)

$
0
0

Thanks @cgrunenberg for pointing this out. I’m struggling a bit with the right JXA (I do all of this in JXA because other parts of my code or plain JS integrating with this). I think it needs to look something like this:

const app = Application("DEVONthink 3");
const db = app.databases['Resources'];

let tags = db.parents.whose({_and: [
	{_match: [ObjectSpecifier().tagType, "ordinary tag"] },
	{_not: [{tags: "" }] }
]})();

tags = tags.map(x => x.location().replace("^/Tags")+x.name())
console.log(tags)

But when I map through the location() and name() it seems it’s querying each individual object again (at least I think it’s doing that if I look at the Script Editor console). Which is probably making it less efficient again.

I tried writing this:

let tags = db.parents.whose({_and: [
	{_match: [ObjectSpecifier().tagType, "ordinary tag"] },
	{_not: [{tags: "" }] }
]}).name();

… which is blazing fast, but as I need the ‘full location’ (location+name) I can’t use that. I’ve created this (somewhat ugly) workaround which leaves most of the work to JS rather than DT - it is blazing fast though.

const app = Application("DEVONthink 3");
const db = app.databases['Resources'];

let tag_locations = db.parents.whose({_and: [
	{_match: [ObjectSpecifier().tagType, "ordinary tag"] },
	{_not: [{tags: "" }] }
]}).location();

let tag_names = db.parents.whose({_and: [
	{_match: [ObjectSpecifier().tagType, "ordinary tag"] },
	{_not: [{tags: "" }] }
]}).name();

tags = tag_locations.map((x,i) => x.replace("/Tags/","")+tag_names[i]);
tags_list = tags.sort().map(tag => { return {id: tag, name: tag.split("/").pop()} })
console.log(tags_list)

Is there a (theoretical) these two queries would deliver items in a different order?

Thanks for thinking along! If there are additional / better ways I’m definitely interested!

EDIT: thanks @cgrunenberg, this is already 6-10x faster!

% time osascript -l JavaScript get-tags.scpt
osascript -l JavaScript get-tags.scpt  0.60s user 0.17s system 41% cpu 1.857 total
% time osascript -l JavaScript get-tags-optimized.scpt
osascript -l JavaScript get-tags-optimized.scpt  0.06s user 0.02s system 28% cpu 0.290 total

Viewing all articles
Browse latest Browse all 17009

Trending Articles