Question on 6/5 Tuesday tip: Even better mail rule script

I was wondering if anyone else is using the script from the June 5 Tuesday’s Tip with success? … le-script/

I’ve set up the script for my rules/paths and it is working nicely except for the fact that it creates duplicates of the email messages and the attachments. Somehow it is looping and processing each email twice, and I don’t know if it is something I have added to the script or a problem with the original. Here is my edited version.

-- 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/Greg/Databases/Berea Arts Council.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 "/Email & Attachments/" & (name of theRule)
		set pMessageLocation to pEmailLocation & "/Messagess"
		set pAttachmentLocation to pEmailLocation & "/Attachments"
		tell application id "com.devon-technologies.thinkpro2"
			if pDatabasePath is "" then
				set destination_database to inbox
				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
					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
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Are multiple rules using this script? Or is the same script executed multiple times for the same messages? A screenshot of the rule might be helpful.

Multiple rules use the script, but it should not be executed multiple times for the same messages as the rule moves the processed emails to another mailbox.

OK, so I made a copy of the script w/new name and made a test rule with myself as the sender, so that only one rule uses this script. The duplicates problem was solved, but the emails are curious. An email with no attachment displays properly:

While basically the same email with an attachment displays this, followed by 4-5 pages of the random characters show in this picture. The attachment looks fine.

Greg, this happens with Mail 6, correct?

Some things are not really working correct with Mail 6 in OS10.8 and I’m still trying to figure out, how to get all my rules working as they did before in 10.7, but I’m short on time.
For example here Mail 6 does not respect the criteria “account is” anymore. I used that to catch all messages to accounts for messages that do not explicitly feature my E-Mail address as a recipient.
Also, if you change a rule, while you have a marked an empty folder in Mail 6 and Mail asks you, if you want to apply the changes to the messages in the selected mailbox and you agree, Mail will process the messages it had processed formerly again, although they are not in that folder anymore! Strange things happening.

I also see some duplicates as you do, but will have to look at all my rules sometime later.

PS: No problems here with attachments. I don’t use “MailTags” or “MailActOn” by the way.

Yes, this is Mountain Lion Mail 6. Interesting part about applying changes to messages again that are no longer in the same folder. That could very well explain why my messages were duplicated. I’m now really curious about the strange formatting of the email w/attachment that I posted above. That’s a deal breaker for me, more so than duplicates. I can always delete a duplicate if needed, but I can’t fix a garbled message.

Does this also happen after adding the message via “Add message(s) to DEVONthink”, see scripts menu extra?

No, it works fine with either that script or the “Add message(s) & Attachments to DEVONthink” script.

OK, so this gets stranger and stranger. First, let me mention that the workflow that I want to accomplish is to have all the captured emails in an email group. I want to be able to take the attachments and file them in other, topical groups in the database. For tracking purposes, I will also add the url link to the original email to the url of each attachment.

Having said that, I added a couple of emails and attachments using the “Add message(s) & Attachments to DEVONthink” script, and both the attachment and the email look fine. I added the url of the email to the attachment, and moved the attachment in the database. Now if I click on the email or double-click on the email to open it in a new window, the email looks fine. However, if I click on the url in the attachment, it opens the email in a new window and the content is garbled as appears above. What the???

Sounds like the stored emails/attachments are fine and that it’s related to the URL. Could you post an example or send me a small database? Thanks in advance!