Importing email from mail.app into a specific group

I would like to modify the script that is supplied with devonthink so that when I import into devonthink it will go into a specific group. I do not need a dialogue that asks which group. I want to hardwire the script so that it will always import into a specific group.

Anyone have any suggestions?

Thanks.

Here’s the version of the DEVON script I modified to import to a specific group. In my case the group is called ‘Periodical contents’. To use it with another group, change the two lines of the script like this one to substitute whatever your group’s called:

         set destinationGroup to create location "/Periodical contents"

(I know that it isn’t efficient to do this twice, but I haven’t had a chance to tidy it up!) Full script is below. I use it with Mail Rule Action, but you should be able to use the same principle for the other import scripts.

HTH,

David.

– Mail Rule Action Import to DEVONthink Pro.
– Created by Christian Grunenberg on Mon Apr 19 2004.
– Modified to import to specific group by David Clough Sep 12 2005
– Copyright © 2004-2005. All rights reserved.

– this string is used when the message subject is empty
property pNoSubjectString : “(no subject)”

– entry point
using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
tell application “Mail”
repeat with theMessage in theMessages
my importMessage(theMessage)
end repeat
end tell
end perform mail action with messages
end using terms from

on importMessage(theMessage)
tell application “Mail”
try
set theDate to the date received of theMessage
set theSender to the sender of theMessage
set theSubject to subject of theMessage
if theSubject is equal to “” then set theSubject to pNoSubjectString

		set theContent to content of theMessage
		if (length of theContent ? 5) then
			
			set AppleScript's text item delimiters to ", "
			set theCC to (address of cc recipient of theMessage) as string
			set theBCC to (address of bcc recipient of theMessage) as string
			set theRecipient to (address of to recipient of theMessage) as string
			set AppleScript's text item delimiters to ""
			
			set theHeaders to "From: " & theSender & return
			set theHeaders to theHeaders & "Subject: " & theSubject & return
			set theHeaders to theHeaders & "Date: " & (theDate as string) & return
			if (length of theRecipient is greater than 0) then
				set theHeaders to theHeaders & "To: " & theRecipient & return
			end if
			if (length of theCC is greater than 0) then
				set theHeaders to theHeaders & "CC: " & theCC & return
			end if
			if (length of theBCC is greater than 0) then
				set theHeaders to theHeaders & "BCC: " & theBCC & return
			end if
			
			tell application "DEVONthink Pro"
				set destinationGroup to create location "/Periodical contents"
				create record with {name:theSubject, type:txt, date:theDate, URL:theSender, plain text:(theHeaders as string) & return & (theContent as string)} in destinationGroup
			end tell
		else
			set theContent to the source of theMessage
			tell application "DEVONthink Pro"
				set destinationGroup to create location "/Periodical contents"
				create record with {name:theSubject, type:txt, date:theDate, URL:theSender, plain text:(theContent as string)} in destinationGroup
			end tell
			
		end if
		set the flagged status of theMessage to true
	end try
end tell

end importMessage

Hi,

the script of davidclough do not work. So I again ask the question:

Which script do you want to modify? There are actually 3 - import selected messages, imported selected mailboxes and an action script. The second one contains the destination:

– this string defines the location for the mails to import
property pLocation : “/Apple Mail”

I mean
“Add message(s) to DEVONthink.scpt”

I would like to add messages with this script to a specific group.

Greetings
Ricklef

This script stores the messages in the group “Apple Mail” for example:


-- Import selected Mail messages to DEVONthink Pro.
-- Created by Christian Grunenberg on Mon Apr 19 2004.
-- Copyright (c) 2004-2006. All rights reserved.

-- this string is used when the message subject is empty
property pNoSubjectString : "(no subject)"

-- this string defines the location for the mails to import
property pLocation : "/Apple Mail"

-- entry point
tell application "Mail"
	try
		tell application "DEVONthink Pro"
			if not (exists current database) then error "No database is in use."
			set theLocation to create location pLocation
		end tell
		set theSelection to the selection
		if the length of theSelection is less than 1 then error "One or more messages must be selected."
		repeat with theMessage in theSelection
			my importMessage(theMessage, theLocation)
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			try
				display alert "Mail" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

on importMessage(theMessage, theLocation)
	tell application "Mail"
		try
			set theDateReceived to the date received of theMessage
			set theDateSent to the date sent of theMessage
			set theSender to the sender of theMessage
			set theSubject to subject of theMessage
			if theSubject is equal to "" then set theSubject to pNoSubjectString
			
			set theContent to content of theMessage
			if (length of theContent ? 5) then
				
				set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
				set theCC to (address of cc recipient of theMessage) as string
				set theBCC to (address of bcc recipient of theMessage) as string
				set theRecipient to (address of to recipient of theMessage) as string
				set AppleScript's text item delimiters to od
				
				set theHeaders to "From: " & theSender & return
				set theHeaders to theHeaders & "Subject: " & theSubject & return
				set theHeaders to theHeaders & "Date: " & (theDateSent as string) & return
				if (length of theRecipient is greater than 0) then
					set theHeaders to theHeaders & "To: " & theRecipient & return
				end if
				if (length of theCC is greater than 0) then
					set theHeaders to theHeaders & "CC: " & theCC & return
				end if
				if (length of theBCC is greater than 0) then
					set theHeaders to theHeaders & "BCC: " & theBCC & return
				end if
				
				tell application "DEVONthink Pro" to create record with {name:theSubject, type:txt, creation date:theDateSent, modification date:theDateReceived, URL:theSender, plain text:(theHeaders as string) & return & (theContent as string)} in theLocation
			else
				set theContent to the source of theMessage
				tell application "DEVONthink Pro" to create record with {name:theSubject, type:txt, creation date:theDateSent, modification date:theDateReceived, URL:theSender, plain text:(theContent as string)} in theLocation
			end if
			set the flagged status of theMessage to true
		end try
	end tell
end importMessage

Hi,

is it possible to tell the script to ask in which group that message should go?

Sure. Just replace…


set theLocation to create location pLocation 

…with…


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

Thanks. Must have been a dum question. But now another one: I’d like a pop up for adding tags in the comments field.

Is that feasible?

Thanks Thomas

You can find a complete script to add comments here:
http://www.devon-technologies.com/support/academy/dt_scripts.html

Shouldn’t be too difficult to combine the scripts.

Not for me :frowning:

Tried it but … in vain. I always asks m to select something. No idea how to tell the script what to do in order to achieve that messages can get tagged during/after import.

just in case it helps anyone else, a copy and paste of some of these scripts in safari munges the greater-than-or-equal-to sign in the script. If you get an error on the ‘?’ in an if statement in the script, that’s the culprit. I just changed it to a greater than sign and it works very well.

Thanks for this – I was fighting with the default mail rule script and this helped me a lot.