AppleScript question re return value of whose

This code

tell application id "DNtp"
	set result to (contents of databases) whose name contains "xxx"
end tell

creates a “list of lists” structure in result: One list for each database, and in each of these lists the matching records found in that database:

{{}, 
{content id 82535 of database id 5}, 
{content id 8412 of database id 7, content id 8374 of database id 7, …}, 
{}, 
{content id 2873 of database id 4, content id 5178 of database id 4,…}, 
{}, 
{}, 
{}}

I thought (!) that whose should return a single list, namely of the records it found. Is this return construct a feature of whose or a specialty of DT? In my mind, a list of records would be perfectly fine since they contain a database property.

It’s not specific to DEVONthink, in this case whose is applied to a list of lists. And therefore the result looks the same.

Thanks for the clarification – the examples I found where only simple ones.

.flat() and .flatMap(), are, of course, helpful here in JS.


Or in AppleScript, perhaps something like:

on run
    flatten({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})
end run


on flatten(xs)
    script
        on f(vs)
            if list is class of vs then
                set n to length of vs
                set a to {}
                
                repeat with i from 1 to n
                    set a to a & f(item i of vs)
                end repeat
            else
                vs
            end if
        end f
    end script
    
    f(xs) of result
end flatten

The question originally arose in a JS script :wink: then I saw that the same thing happens in AS.

flat etc would be ok here if it returned a one-level list in all cases, i.e. regardless of the nesting of objects whose is called on.

Probably worth experimenting with its depth argument, which defaults to 1, but to which you can supply the special quasi-integer value Infinity.

Array.prototype.flat() - JavaScript | MDN

Infinity - JavaScript | MDN