Move items maintaining same group structure

I have an email archiving system that exports into an indexed folder where the mailbox structure becomes a folder structure, which ends up as a Groups structure. The problem I have is, after I move emails into the database, I don’t want them connected to the Finder folder structure and I can’t work out how automate relocating items retaining the same path. Say the indexed Finder folder has this:

  • Mailbox_A/
    • Mailbox_A_A/
      • Email 0001
      • Email 0002
      • etc
    • Mailbox_A_B/
      • etc
  • Mailbox_B/
    • etc

And in DT I have a database for emails like this:

  • Inbox (Indexed Finder folder)
    • As above: Mailbox_A/ etc
  • Email Archive
    • Mailbox_A
      • Mailbox_A_A
      • Mailbox_A_B
    • Mailbox_B
    • Mailbox_C

Is there a way to make a Smart Rule that moves everything from the indexed folders to Email Archive and either puts them into the correct existing sub-groups or makes group structures if they don’t exist?

“Just drag to the imported group” doesn’t work because:

  1. Most mailboxes are incrementally exported over time.
  2. Moving an indexed folder into DT works the first time but more emails to the indexed folder I end up with duplicate Groups hell.
  3. Archiving emails is an ongoing, tedious task and I need it automated/easy/reliable

On the face of it, this should be easy to accomplish but…

  • Is the Email Archive a separate database?
  • Are you adding more mailboxes and submailboxes to the indexed folder?

What about DEVONthink’s native mail import?

To your question: I’m not sure if this can be done with standard smart rule actions, but it should be easy enough with a smart rule script.

The details of your setup matter, so it would be good to answer @BLUEFROG’s questions.

Here is an example, assuming the finder folder is indexed in the inbox of your “Email Archive” database (not the global inbox).

JavaScript:

function performsmartrule(records) {
	const app = Application("DEVONthink");
	app.includeStandardAdditions = true;
	
	const indexedParentName = "Test - Email Export";
	// RegEx to avoid problems with group names containing "/"
	const pathSeparator = /(?<!\\)\//g;
	
	records.forEach(r => {
		const locParts = r.location().split(pathSeparator);
		// --> ["", "Inbox", "indexedParent", "Mailbox_A", ... , ""]
		const cutoff = locParts.findIndex(n => n === indexedParentName) +1;
		const subLocation = locParts.slice(cutoff).join("/");
		// --> "Mailbox_A/.../"
		const dest = app.createLocation(subLocation, {in: r.database});
		app.move({record: r, to: dest});
	})
})()
1 Like