If you only need all the records in a database (without knowing their place in the group hierarchy), you can use its contents
property:
(() => {
const app = Application("DEVONthink 3");
const db = app.databases['Test'];
db.contents().filter(c => c.type !== 'group' && c.type !== 'smart group').forEach(c => {
console.log(`${c.name()} in "${c.location()}"`)
})
})()
contents
contains all records, including groups and smart groups. Those two are first filter
ed out and then forEach
loops over the real documents. That approach will, however, also find documents in the trash. One can filter them out by comparing their locationGroup
’s UUID to the database’s trashGroup
UUID, for example.
A similar approach can return all records in all databases by including the code from db.contents()
in a app.databases().forEach(db => {...})
Your approach works well, of course. I’m just not a big fan of recursion. And I’d suggest to use forEach
over for
. Especially, if you introduce a function-local variable in your for
loop by declaring the control variable as a var
:
https://www.reddit.com/r/javascript/comments/a50jte/is_it_best_to_use_var_or_let_in_for_loop/
forEach
prevents you from inadvertently introducing function-scoped variables, requires less typing, and it can be chained with other Array
methods (like filter
, map
etc).