Quantcast
Viewing all articles
Browse latest Browse all 16267

Most efficient way to get all nested tags (JXA)

Paging @chrillek and other JXA experts. What’s the most efficient way to get all the (nested) tags in the correct order (based on their nested properties, so similar to the order in the sidebar) in an array with objects that like {id: "apps/pkm/devonthink", name: "devonthink"}

I’ve created the following recursive script but I’m wondering if there’s a more efficient / optimized way (to always lower time spent when executing scripts). Thanks!

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

function getChildren(item, tags) {
	item.children.whose({_match: [ObjectSpecifier().type, "group"]})().forEach(c => {
		tags.push(`${c.location().replace('/Tags/','')}${c.name()}`);
		if (c.children.whose({_match: [ObjectSpecifier().type, "group"]})().length > 0) {
			getChildren(c, tags)
		}		
	})
	return tags;
}

tags = getChildren(db.tagsGroup, []);
tags_list = tags.sort().map(tag => { return {id: tag, name: tag.split("/").pop()} })

I thought about away to not need sort() afterwards, but as items are unordered I need to sort at least once.


Viewing all articles
Browse latest Browse all 16267

Trending Articles