Import specific email and attachments

Hi there,

I am sorry but I am feeling quite lost. This is my first detail foray into scripting DEVONthink. (I am using DT 4)

I want to create a rule in apple mail which imports mail and attachments from my PhD supervisors into the “Supervisor Emails” group.

I have set up the following rule


And here is my code which in basically the standard “Add message(s) & attachments to DEVONthink.SCPT” script, with my database and group names.

-- Import selected Mail messages & attachments to DEVONthink.
-- Created by Christian Grunenberg on Mon Mar 05 2012.
-- Copyright (c) 2012-2020. All rights reserved.

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

tell application "Mail"
	try
		tell application id "DNtp"
			if not (exists "PhD") then error "No database is in use."
			set theGroup to "Supervisor Emails"
		end tell
		set theSelection to the selection
		set theFolder to (POSIX path of (path to temporary items))
		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, theFolder, theGroup)
		end repeat
	on error error_message number error_number
		if error_number is not -128 then display alert "Mail" message error_message as warning
	end try
end tell

on importMessage(theMessage, theFolder, theGroup)
	tell application "Mail"
		try
			tell theMessage
				set {theDateReceived, theDateSent, theSender, theSubject, theSource, theReadFlag} to {the date received, the date sent, the sender, subject, the source, the read status}
			end tell
			if theSubject is equal to "" then set theSubject to pNoSubjectString
			set theAttachmentCount to count of mail attachments of theMessage
			tell application id "DNtp"
				if theAttachmentCount is greater than 0 then set theGroup to create record with {name:theSubject, type:group} in theGroup
				set theRecord to create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string), unread:(not theReadFlag)} in theGroup
				perform smart rule trigger import event record theRecord
			end tell
			repeat with theAttachment in mail attachments of theMessage
				try
					if downloaded of theAttachment then
						set theFile to theFolder & (name of theAttachment)
						tell theAttachment to save in theFile
						tell application id "DNtp"
							set theAttachmentRecord to import path theFile to theGroup
							set unread of theAttachmentRecord to (not theReadFlag)
							set URL of theAttachmentRecord to theSender
							perform smart rule trigger import event record theAttachmentRecord
						end tell
					end if
				end try
			end repeat
		on error error_message number error_number
			if error_number is not -128 then display alert "Mail" message error_message as warning
		end try
	end tell
end importMessage

the line

if theAttachmentCount is greater than 0 then set theGroup to create record with {name:theSubject, type:group} in theGroup

throws the following error in Script debugger.

DEVONthink got an error: Can’t make “Supervisor Emails” into type parent or missing value.

I am sorry if this seems like a newbie question

This is just a string, not a group. See e.g. command get location at to retrieve a certain group.

Thanka for your help

I could not find any info on get location at.

But I did find get record at which seemed to work.

In the end, I got this working by importing emails into the global inbox and then using smart rules to move them to my chosen folder.

This is better than my initial plan, because I can use the same script for different email groupings.

Thanks again for your time.

Manny

1 Like

You’re right of course, I’m sorry. Too many commands in the script suite of DEVONthink 4 :wink: