Regex Question for Revision to Mail/Devonthink Script

I would like to set up my Apple Mail to automatically sort specific emails into particular DT3 groups.

I think a simple way to do this would be to modify the existing “Add message(s) & attachments to DEVONthink” script.

My plan would be to include x-devonthink links as part of the Subject line for any email I want to file in Devonthink. Then set up a Rule in Apple Mail so if x-devonthink is in the subject line then the script is run.

I am pretty clear on how to edit the existing script - except I need to figure out how to use Regex or Sed or some other solution to extract the UUID from an x-devonthink link contained in the email subject. Then I can assign that uuid to a variable in the applescript and assign that as the group.

Thoughts on how to best accomplish this?

**** Thinking further- is this better done simply with smart rules by creating a unique naming system for the relevant Groups and then Then using Scan/Regex and Move in a Smart Rule?

My plan would be to include x-devonthink links as part of the Subject line for any email I want to file in Devonthink. Then set up a Rule in Apple Mail so if x-devonthink is in the subject line then the script is run.

This seems a bit much for a mail rule attribute.

Have you looked at the File Items using @Destination in Name smart rule script?

1 Like

That’s getting into the general ballpark… but I need to put the identifier in the Subject line rather than the name because the idea is that way I would automatically capture an entire email thread; it is much easier to add something to the Subject line than to change my reply-to address on a per-thread basis.

I suppose I can edit that script to search for @Destination in Subject rather than Name - that may well be much easier than reinventing the wheel. I will give it a try - thank you.

but I need to put the identifier in the Subject line

The Subject line is the name when the file is imported.

it is much easier to add something to the Subject line than to change my reply-to address on a per-thread basis.

I’m not sure why you’d be thinking of doing this.

OK - even simpler. The task is basically done!

How does it handle duplicate or non-unique group names, i.e. if I do @Smith and I have groups for @Smith Bob and @Smith John does it simply find the first match?

You should be explicit in what you put in the Subject line and it should be the last item in the line.

image

PS: In order to process groups in any database, edit the script and set the property pGlobalSearch to true.
You will need to quit and relaunch DEVONthink for this to take effect.

Thank you for the tip

That sounds like exactly what I am trying to do - should be extremely helpful

Totally agree that what you say above is best practice. That said, does the Scan only search at the end of the subject or in the entire subject? And what does happen if the File to @@ is not unique?

@BLUEFROG

I played a bit with the File Items script. This looks extremely promising for my use case (thank you) but there is one tweak I am trying to make.

First - It shows in the Smart Rule editor as File Items using @Destination in Name but in my Scripts folder it is simply called “File Items.scpt” - is that the correct script?

Second - the existing script searches for an exact match of group name and if it does not find it, then it creates a new group. I am hoping to change that behavior for a “contains” search rather than an “equals” search.

So as written this item goes into new Group Test5555:
Test @Test5555

And as written if I then add this second item then it goes into new Group Test55:
Test @Test 55

I would like to change the behavior and instead only search to see if the Name is contained within an existing group name. In the above example, I would want Test @Test55 to go in the already existing Test5555 group. I tried the edit below to replace the commented line with the line immediately below it - but the behavior does not change.

How can I change this to match for the content after the @ in the Name is contained in an existing group name rather than matching it exactly?

image