Mail Import Scripting

I’ve been trying to figure out a way to script import of messages from Mail and I am not getting anywhere with it. So, I thought that I should ask for help here.

There are 2 scripts that I am trying to combine results from. First is the “Copy selection to…” script from /User/Library/Scripts/Devonthink Pro and the second is “Mail Import” from DevonThink Pro’s script menu.
First script takes selected message, prompts for a group to import to, then imports as RTFD with attachments if there are any. Unfortunately, it also imports HTML mail as an attached HTML file instead of parsing and improrting as RTF. Another negative of this script is that if you have more then 1 message selected it imports all of them as one record.
Second Just imports all messages from the mailbox as TXT loosing any attachments. I finally found a modification to that script here in the forum where it will only import selected mailboxes instead of all mailboxes.

What I am trying to achive is the following:

  1. Prompt for a mailbox or a message to be selected. Also work with multiple mailboxes or messages selected and import them separately, not as one record.
  2. Prompt for a group to import to. Import messages in that group. Import Mailbox as a new group inside selected group with messages below new group.
  3. Import as RTF if no attachments parsing HTML mail and importing it as text not as file or RTFD if there are attachments.

I am a very-very-very begininer in Apple Scripting so I am asking for help. Does anyone have a script like that or is willing to help create one?

Very much appreciate any help I can get.

Thank you

The first two steps should be possible, e.g. the second one can be implemented via

set theDestination to display group selector "Destination" buttons {"Cancel", "OK"}

but the third one is probably impossible. You can’t transfer rich text between applications using AppleScript (it’s a limitation of Cocoa/Mac OS X) and you can’t save mails via AppleScript (e.g. to import them afterwards to DEVONthink Pro). The only thing one could do is to save all attachments and import them as separate files but not as RTFD.