Rule to change dates on group based on content

Hi

I am trying to find the best (for my case) suited approach of handling mails. When I import an entire thread from mail, each mail is separated in DT. Then I group the mails in DT, but now I have a group with current creation and modified date. This of course makes perfect sense as the group was just created!
I can then change the creation date of the group to the date of the first email in the thread, and the modification date of the group to the last email in the thread. For now that seems like a good solution for my needs.

Could this manual modification of the group be handled by scripting?

I am aware of the “Import complete conversations” and “Group conversation threads” option in the preferences, but they do not seem to do it for me.

Thanks for any advice or input

Possibly. If date of the email that you’re referring to is available. Which date is that, in fact – sent or received or opened?

That’s possible of course:

tell application id "DNtp"
	set theGroup to selected record 1
	set theChildren to children of theGroup
	if theChildren is not {} then
		set creation date of theGroup to (creation date of item 1 of theChildren)
		set modification date of theGroup to (modification date of item -1 of theChildren)
	end if
end tell
1 Like

Perhaps this example (screenshot) explains what I’m trying to achieve. I’m using the “Created” timestamp of the mail to set the timestamps on the group

EDIT @cgrunenberg answered while I was posting

Thanks!

@cgrunenberg After running the script on the group, the dates changed but not the way I wanted. Can you figure out what happened?

The script simply uses the first & last child of the group, not necessarily what you’re seeing unless you’re using View > Sort > Unsorted

Ah I see. I sort by “Created”. Unsorted doesn’t follow the timestamps hierarchical

I suggest this JavaScript, following @cgrunenberg’s AppleScript version:

(() => {
  const app = Application("DEVONthink 3")
  const group = app.selectedRecords()[0];
  const children = group.children().sort((a,b) => a.creationDate() - b.creationDate());
  if (children.length) {
    group.creationDate = children[0].creationDate();
    group.modificationDate = children[children.length-1].creationDate();
  }
})()

It sorts the group’s children by creation date and uses the creationDate of the first record to set the group’s creationDate and the one from the last record to set the group’s modificationDate.

Is that what you’re looking for?

1 Like

@chrillek That seems to work flawlessly :pray:

I have created a “Smart Rule” to run manually on the groups. Is that the “correct” way of doing this? I ran the Applescript from the script menu, but can’t figure out if javascript can be used the same way.

Thanks for helping me out

For that, you have to save the code from script editor into the appropriate folder. It will not work with a simple text file, the extension scpt is required. Which script editor provides automatically when saving.

Hi, sorry for arriving late on this thread. The script works great for groups which don’t have both subgroups and items… but I can’t work out how to get it to work for subgroups, and then to pull the modification date for the parent group from the latest of all the subgroups/items. Is it possible?

Here is a script that recursively updates groups’ creation and modification dates:

(() => {
  function updateGroupDate(g) {
    for (let c of g.children()) {
	  if (c.type() == 'group') {
	    updateGroupDate(c);
	  }
	}
	
    const children = g.children().sort((a,b) => a.creationDate() - b.creationDate());
    if (children.length) {
      g.creationDate = children[0].creationDate();
      g.modificationDate = children[children.length-1].creationDate();
    }
  }

  const app = Application("DEVONthink 3")
  const selected = app.selectedRecords()[0];
  if (selected.type() == 'group') {
    updateGroupDate(selected);
  }
})()
1 Like

Welcome @w1ndy

Thanks for sharing your thoughts.

1 Like

Thank you!