Create nested groups based on a string like "GroupA/GroupB/GroupC"

I have searched before asking this, but couldn’t find, what I was looking for. I would like the following:

  • I have a path of the form “groupA/groupB/groupC”
  • I would like to recreate this hierarchy using groups within DevonThink, without having to create and enter a name to every (sub)group of the path
  • It should also not create duplicate groups, when I later enter a path like “groupA/groupB/groupD”

To clarify:
groupA/groupB is a location, not a path.

  • It should also not create duplicate groups, when I later enter a path like “groupA/groupB/groupD”

It’s unclear what you mean here.

I thought it’s possible to have multiple groups of the same name that live next to each other in the hierarchy. So let’s assume there would be some kind of script, that creates (sub)groups based on the string GroupA/GroupB/GroupC. That script would then go and create a structure like this in DevonThink:

  • GroupA
    • GroupB
      • GroupC

The second time I run this script, using the second string (“GroupA/GroupB/GroupD”), I would want the hierarchy not to look like this:

  • GroupA
    • GroupB
      • GroupC
  • GroupA
    • GroupB
      • GroupD

But instead, it should look like this:

  • GroupA
    • GroupB
      • GroupC
      • GroupD
tell application id "DNtp"
	set groupNames to {"Group C", "Group D"}
	repeat with groupname in groupNames
		create location ("/Group A/Group B/" & groupname)
	end repeat
end tell

yields this in the root of the database…

image

1 Like

I think I get the problem here. If you create the whole path/location each time you run your script, then yes, DT will do as you asked and create each group in the location specified each time.

The trick is checking to see if the location exists already in the midst of your location creation. Roughly:

try
    get record at 
on error
    make new record at
end try 

The question, then, is how you want to determine whether or not to create a new group vs use a previously created one.

1 Like

@BLUEFROG Thanks, but this seems not dynamic enough. I basically look for a functionality like mkdir -p (man page).

@ryanjamurphy I think you’re on the right track with your idea.

I tried to put what has been posted together in pseudoscript, putting into square brackets, what I don’t know how to do.

tell application id "DNtp"
   set groupNames to [make string to array such as first element is first part until slash, etc.]
   repeat with groupname in groupNames
              [if element exists as a group do nothing
              else create element:
                   if element is not first element of groupNames
                       get last element and create group as a subgroup to it
                   else
                       create location
              save current location as last location]
   end repeat
end tell

EDIT: last refers to last seen not last in the array.

Something like that?

There’s no need to check for the existence as create location will not create a redundant location.

Are you just trying to create a predetermined group hierarchy in the current location?

1 Like

I think so. I have a universal hierarchy which is the same in every context where such a thing makes sense (filesystem(s), Zotero, Mailboxes, etc.) this hierarchy grows with time, when new things are added. I would like to be able to have an easy way to “instantiate” parts of the hierarchy if I have documents in DT that need to be filed away. Does this make sense? It would be great to get a pop-up, paste a string of the described form in there and DevonThink (or a script) creates the appropriate (sub)groups, if needed and otherwise only adds the “leaf group”.

Have you considered just creating the group hierarchy and exporting it as a template via File > Export > as Template?

Yes. That would mean maintaining the hierarchy in more than one spot. That’s not what I want to do. My notes system holds the “blueprint” for the whole hierarchy and tools like DevonThink use the parts of the hierarchy that they actually need.

Screencaps and examples could be helpful.

Okay I figured it out myself. It was super simple in the end:

What you see is a KeyboardMaestro Macro, which prompts the user for a string of the form as described and then simply adds (sub)groups to the database root, if they don’t exist already. This is exactly what I needed.

@BLUEFROG I have a looong list like this:

  • Blog
    • Drafts
    • Published
  • Projects
  • Setup
  • xx-Archive
    • Mails
    • Pictures
    • Taxes

Let’s assume that I would like to archive my email and my tax files in a DevonThink database, then I would need to create the following (sub)groups in my dedicated DT-DB:

  • xx-Archive
    • Mails
    • Taxes

I would not need anything else in that DB, but I would also like to not reinvent the wheel with every other instance I want to file something. If later I would like to archive some Pictures in the same DB, I would already know where they would go: xx-Archive/Pictures, naturally.

But xx-Archive/Pictures might also be useful somewhere else (maybe old screenshots accompanying published blog posts should be filed away on the server there?).

Obviously, these examples are a little far fetched, but I hope you get the idea.

1 Like