modification of "Automatically file messages & attachments"

I use the following script in Apple Mail attached to a rule. For different reasons I want to deactivate my rules in Apple Mail and instead run this script manually , from external (Keyboard Maestro).

To you helpful souls here: What would I have change in the code to make this happen?

-- Automatically file messages & attachments
-- Created by Christian Grunenberg on Fri May 25 2012.
-- Copyright (c) 2012. All rights reserved.

-- POSIX path of destination database
property pDatabasePath : "/Users/me/Documents/DEVONthinkPro/datenbanken/Studio_mail.dtBase2"

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

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		-- Location of destination groups.
		set pEmailLocation to "/Studio/Active"
		set pMessageLocation to pEmailLocation
		set pAttachmentLocation to "/Attachments_Studio"
		
		tell application id "com.devon-technologies.thinkpro2"
			if pDatabasePath is "" then
				set destination_database to inbox
			else
				set destination_database to open database pDatabasePath -- Ensure that the database is open
			end if
			set email_group to create location pEmailLocation in destination_database
			set message_group to create location pMessageLocation in destination_database
			set attachment_group to create location pAttachmentLocation in destination_database
		end tell
		
		tell application "Mail"
			set theFolder to (POSIX path of (path to temporary items))
			repeat with theMessage in theMessages
				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
					
					tell application id "com.devon-technologies.thinkpro2"
						set theRecord to create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string)} in message_group
						set unread of theRecord to (not theReadFlag)
					end tell
					
					repeat with theAttachment in mail attachments of theMessage
						set theFile to theFolder & (name of theAttachment)
						tell theAttachment to save in theFile
						tell application id "com.devon-technologies.thinkpro2"
							set theAttachmentRecord to import theFile to attachment_group
							set unread of theAttachmentRecord to (not theReadFlag)
						end tell
					end repeat
					move theMessage to mailbox named "DEVONthink"
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Thanks for listening!

Bernd

I believe you’ll make faster work if you adapt the script “Add message(s) and attachments to DEVONthink.scpt” that is packaged with the DEVONthink download – here’s a copy of that script:

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

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

tell application "Mail"
	try
		tell application id "com.devon-technologies.thinkpro2"
			if not (exists current database) then error "No database is in use."
		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)
		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)
	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 "com.devon-technologies.thinkpro2"
				set theGroup to incoming group
				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)} in theGroup
				set unread of theRecord to (not theReadFlag)
			end tell
			repeat with theAttachment in mail attachments of theMessage
				set theFile to theFolder & (name of theAttachment)
				tell theAttachment to save in theFile
				tell application id "com.devon-technologies.thinkpro2"
					set theAttachmentRecord to import theFile to theGroup
					set unread of theAttachmentRecord to (not theReadFlag)
					set URL of theAttachmentRecord to theSender
				end tell
			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

What you then want to do is modify this script to include the custom properties for database, subject, and groups:

-- POSIX path of destination database
property pDatabasePath : "/Users/me/Documents/DEVONthinkPro/datenbanken/Studio_mail.dtBase2"

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

-- Location of destination groups.
set pEmailLocation to "/Studio/Active"
set pMessageLocation to pEmailLocation
set pAttachmentLocation to "/Attachments_Studio"

and finally include this logic (in a “tell DEVONthink” block probably).

set email_group to create location pEmailLocation in destination_database
	set message_group to create location pMessageLocation in destination_database
	set attachment_group to create location pAttachmentLocation in destination_database

Either starting with the code you posted, or the code I suggested above, there’s a fair amount of refactoring involved. It might be simpler just to use the built-in script I posted without making any modifications?

Sorry, I haven’t tested this – it would involve instrumenting a test bench to meet all the custom routing features in your code.

Hi korm,

thank you for your help!

I had hopes it could be only for a different handler to make this script working from external and prior to my posting I had already tried in that direction.
To import the email and attachments to the Global Inbox with the supplied script is the last option for me and I would rather rethink my whole configuration instead.

Due to my massive, personal shortcomings with Apple Script I didn’t yet manage to successfully combine all parts you gave me and I won’t present the results here and ask for further attendance on that. Maybe I’ll have more luck another day, when I’ll try again. :wink:

Happy new year!
Bernd