AppleScript: Get every group in database(s)

I’m trying to write a script to get a list of every group in my databases. Here are two methods that almost work but not quite:

set x to every record of every database whose kind is "Group"
– This gets only top-level records (no subfolders)

set x to every parent of every database
– This gets only groups with contents (no empty groups)

Is there an easy way to get both subgroups and empty groups in one list? If not then presumably I have to write a loop to get the first layer of groups, get every sub-group, then repeat with each subsequent layer. (I can see how it’d work but seems very inefficient for something that should be quite simple.)


This should return all parents (groups & empty groups but also ordinary tags). And I just tried this successfully using the global inbox and a new, empty group:

tell application id "DNtp" to return name of every parent of inbox

To filter out ordinary tags, use e.g.

tell application id "DNtp" to return every parent of current database whose tag type is not ordinary tag

Ah, I must have been getting confused! Thanks very much.