script for Email to Specific database

This may be in the forum, but for the life of me I cannot find it.
It seems there should be a way to set up a rule in Mac Mail that points to a script that will send those mails that fit the rule to a specific database in DTPO.

The Only Script I see is “Add message to DEVONThink Pro”. So it sends it to the general inbox. Which means I then have to set up more rules in the general inbox to send to specific Databases.

This seems silly.
Seems there should be a way for the script to designate which DB I want to send it to.

Same applies for . “Import to selected group” doesn’t give me an option to choose which group (aka database) I want to send it to.

Seems I should be able to tweak the script to point to a specific DB.

Am I missing something simple here or do these options not exist?

You are not missing anything and you’re more than welcome to break the script however you see fit. 8) Bear in mind that Apple Mail Rules are obnoxiously inconsiderate (yes, I mean inconsiderate in that they appear to work by whim or solar cycles).

Thanks for the replay Bluefrog. Alas I am not a script writer, hence I was looking for one that existed or at least one that someone modified to do this so I could at least use it as a template and tweak it to my needs.

Re-reading what you wrote, Bluefrog, and maybe I wasn’t clear. The script that DEVON supplies to sent email to a database does not allow me to specify which database it goes to. This is, in my understanding, not a function or problem with MacMail (regardless of whether the rule is initiated) but a function of how the script works, or in this case world half way.

Any hints on how to write, or amend, an existing script that sends mails (according to a rule which we admit may or may not kick in) to a specific DT in Devon are welcomed.

I have modified the “Mail rule - Add messages to Devonthink”, The Mail-rule triggers this Applescript if conditions are met. The Applescript is a brut force one, since my scripting knowledge is minimal. If a mail qualifies, it investigates Devonthinks current database. If this is not the desired one it closes all databases and open the desired one (in my case “Hagaberg” and then inserts the messages in group "emails.
In spite of its clumsiness it does work!
The modified script goes in the same location as other mail-scripts, i.e. ~/Library/Application Scripts/
Script-file “Mail Rule - Add Hagaberg mail to Devonthink” follows;
– Mail Rule - Add messages to DEVONthink Pro.
– Created by Christian Grunenberg on Mon Apr 19 2004.
– Copyright © 2004-2012. All rights reserved.

– This script modified by Gul to allow mail in mailbox “Hagaberg” to be inserted into DevonThink database “Hagaberg” group “emails”

– this string is used when the message subject is empty
property pNoSubjectString : “(no subject)”
– this added by Gul 2013-12-30
property pHagaberg_database : “/Users/gunnar/Documents/DTPO_Databaser/Hagaberg.dtBase2”

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
tell application “Mail”
repeat with theMessage in theMessages
– display dialog “Dealing with mail”
tell theMessage
set {theDateReceived, theDateSent, theSender, theSubject, theSource, theContent, theReadFlag} to {the date received, the date sent, the sender, subject, the source, the content, the read status}
end tell
– if theReadStatus then display dialog “The message IS received”
if theSubject is equal to “” then set theSubject to pNoSubjectString
tell application id “com.devon-technologies.thinkpro2” – closing all databases causes the Inbox to be set as “current database” Gul 2013-12-30
– display dialog "Entering DevonThink, current database is: " & (name of current database as string)
if ((name of current database as string) is not “Hagaberg”) then
if ((name of current database as string) is not “Inbox”) then
– display dialog "Current database is: " & (name of current database as string)
if ((name of current database as string) is not “Inbox”) then
delay 0.5
close current database
exit repeat
end if
end repeat
end if – current database is now “inbox”
delay 0.5
– now, open Hagaberg to set “current database” to “Hagaberg” Gul 2013-12-30
open database pHagaberg_database
delay 0.5
end if – current database is now “Hagaberg”
– display dialog "Current database is now: " & (name of current database as string)
set myGroup to create location “/emails/” – location where we want the mails to collect Gul 2013-12-30
–display dialog “Ready for DEVONThink”
delay 0.5
set theRecord to create record with {name:theSubject & “.eml”, type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:theSource} in myGroup
delay 0.5
set unread of theRecord to true
end tell
– Move the theMessage from the Inbox to Hagaberg, note NO “move” nor “save” JUST “set”
set mailbox of theMessage to mailbox “Hagaberg”
delay 0.5
end try
end repeat
end tell
end perform mail action with messages
end using terms from

When scripts are posted here it is helpful for others who might want to read and copy them if the code is surrounded by "```
", “{/code}” tags. (Without the quotation marks, of course.) Thanks!

Thanks gul-iMac. Just some clarification; certain parts of the script refer to ‘Gul 2013-12-30’, such as;

"[set myGroup to create location “/emails/” – location where we want the mails to collect Gul 2013-12-30]

I am not sure what that part is referring or pointing to. Is that the name of the Folder your DB are in, or is that a non functioning note you put in to distinguish the part you’ve written?

Hey thanks Gul. I figured it out.

And your right, that does work.

Thanks so much!!! This will saves mea ton of time and help keep all my projects orderly.

I am surprised this is not a ‘base’ script that Devon supplies. To me it seems like a no brainer to have.

Again, Thanks.

The “Gul-x”-thing is just my date stamp.
Glad it works for you.
My only problem is some randomness of the OSX Mail-program to properly parse the rule-set and activate the script. But that is another problem.


Yeah Mail Rules have always been flakey. But at least this allows me to have some confidence that the my mail will go to the correct DT. I can always initiate a manual run of the rules once in a while. I find doing that gets the mail to ‘follow the rules’ when implemented.

PS I have already written 3 different scripts based on your template and could’t be happier.