That’s one of the cases where a JavaScript version is a lot shorter and simpler:
(() => {
const app = Application("DEVONthink 3");
const currentApp = Application.currentApplication();
currentApp.includeStandardAdditions = true;
const currentWindow = app.viewerWindows[0];
let tagSet = new Set();
currentWindow.root.children().forEach(record => {
tagSet = tagSet.union(new Set(record.tags()))
})
const sortedTags = Array.from(tagSet).sort();
const chosenTags = currentApp.chooseFromList(sortedTags,{ withPrompt: "Specify Tags", multipleSelectionsAllowed:true});
console.log(chosenTags);
})()
Fun facts:
- There’s a
Set
datatype in JavaScript, so we use that instead of checking if an element exists already in the tag list - We can use the
union
method on theSet
object, since Apple has that already implemented in its JavaScript engine. It (the method) takes care of adding every element only once. - From the
Set
, we can easily build anArray
object, which can then besort
ed with the aptly named JavaScript method
That is not to say that the AppleScript code is bad – just a bit chatty