Mail applescript not working

I am trying to figure out why mails are not moving from Apple Mail into DTP. Would enjoy automating the transfer of an email from one to the other, which is the promised functionality of the DT Apple Mail plugin.

Dragging and dropping is doable but it is not automatic.
Using the Email importer is doable but it is not automatic.

I would like to be able to setup one simple rule, a mail from abc@abc.com (example), when landing into the AppleMail inbox, is recognized and automatically moved to the DTP inbox. That is what I have been writing about in this extensive post about something that should be simple and it is not.

That is why I am looking into the internals of the plug in.

My vehicle is not running, I donā€™t have a mechanic, I need to move, so I lift the hood. Do I know what I am doing? No! Do I know what I want? Yes. I want to move forward. I have much else to do with DTP and my information, so I am now stuck on something that should be simple and I am trying to solve it.

Thank you in advance for your attention to this matter.

1 Like

I ran into the exact same problems as the OP. I just bought DEVONthink 3 Pro to automate my incoming emails to be sent to DEVONthinks inbox so I have all my incoming (electronic) mail in one place. But first, I got confused by the names starting with ā€œAddā€ and ā€œFileā€ like the OP ā€“ it seems they both mean ā€œAddā€, they just create different file types. I think the wording could be improved here.

Next, I ran into an issue with Apple Mail itself (where we have to blame Apple) where when I was trying to apply a rule that both moved a message to a specific folder and ran a DEVONthink script, the script seemed not to work. Iā€™m guessing because Apple mail did the ā€œmoveā€ first (even though I didnā€™t set up to be first) and then the paths were broken. But I found an easy workaround (which is the main reason Iā€™m writing this message), let me share:

Instead of relying on an Apple rule to do both actions (send to DEVONthink & move/delete email), you can simply do both in the AppleScript. To do that, open the folder containing the AppleScript files from DEVONthink ā€“ Apple provides a neat shortcut button ā€œOpen in Finderā€ in the dropdown, see screenshot:

Then duplicate the AppleScript you would like to adjust, rename it (I called it Send to DEVONthink Inbox & move to @DEVONthink mailbox), double-press to open the Script Editor and add the following line right before the closing end repeat of the repeat with theMessage in theMessages body (itā€™s the last end repeat in my case):

set mailbox of theMessage to mailbox "<YOUR_FOLDER_NAME>" of account "<YOUR_ACCOUNT_NAME>"

Donā€™t forget to replace <YOUR_FOLDER_NAME> and <YOUR_ACCOUNT_NAME> with the proper names in your case, e.g. with the folder named @DEVONthink and an account names Gmail it would be:

set mailbox of theMessage to mailbox "@DEVONthink" of account "Gmail"

Hereā€™s a screenshot of what it looks like in full:

If you simply want to delete the email, you might want to use the delete command, but @Blanc strongly recommends against this here below. Instead, itā€™s safer to move mails you want to delete to some ā€œTrashā€ folder with the above method.

Hope it helps.

I strongly recommend against doing this. I have repeatedly experienced Apple Mail identifying a specific email via the rule and applying the actions to a different email. This is so common that I include additional checks within the script to be sure the correct mail is being acted upon. My experience is that this problem occurs when multiple emails arrive at the same time when the Mac wakes from sleep. For me at least, Mail rules are unreliable at the best of times. A quick search of the forum and the internet suggest itā€™s not just meā€¦

1 Like

Iā€™ve had issues with rules in Apple Mail, too. But in my case mostly, that they were simply not applying automatically. But I read somewhere that this might be because Apple Mail isnā€™t always on and not the first client to download the messages, somehow magically skipping the rules for some weird reasons. So what I try now is to let Apple Mail open at all times on my Mac mini which I use as a media server anyways. In my tests throughout the day today, this made rules work reliably. I hope it will stay like that.

But itā€™s a good point to recommend against running delete theMessage, I just updated the post to more clearly warn against it (but still mentioning itā€™s existence in case someone has a use case for it).

1 Like