Import email & attachment to the *same group* via Apple Mail rule script

I’m using DT3’s built-in Apple Mail rule script to automatically import emails to DT. The email and the attachment are successfully imported.

The problem is that they’re not imported into a new group together - which means I can’t tell which attachment arrived with which email. So I can’t file them into a DB.


  1. Is there a way to create a new group in the inbox for each imported email?
  2. Is there a (rules-based) way to automatically identify which attachment(s) belongs with which email, so they can be filed correctly?


I’m having a similar problem. Some e-mails are grouped, but many are not. It would be great if there were a way to replicate the way Apple Mail displays threads

One more suggestion:
I hope the Apple Mail script can also avoid duplicated import. It’s more an issue of Apple Mail since mail rule may re-apply itself when we make adjustment to the criteria and choose apply rule. When this happens, DT3b2 will still import all mails that satisfy the rules as duplicates.

When I amend an existing rule, Mail gives an option to apply or not apply the rule to existing emails (MacOS Mojave). So I don’t see a problem with duplicates?

I understand what you are saying. But an adjusted rule will need to be re-run even for adding one additional sender as criteria. For example, I want to file the emails from a company but later I want to include one more contact from the same company or related contact from other company on the same subject who have also been sending email to me before (happened to me all the time), if I want to keep the mails to export to the the same mailbox group in the DT’s inbox, I need to rerun the import. There is of course always work around, e.g., delete the current groups in DT3 before rerunning the adjusted rule/s.

In any case, I suspect there’s something I’m missing here as (I’d assume!) most usecases for importing email + attachment involve keeping them together, or at least maintaining a relationship.

I’ve tried tinkering with the AppleScript of the built-in script, but haven’t (yet) been able to do what’s needed i.e. create a new group for each imported email, named by subject, in which the group contains the email and that email’s attachments.

Hoping @BLUEFROG might be able to help with this?

I’ve tinkered with the original script, and it appears to now do what I want. i.e.

  • each message is filed with its attachments into a new group named after the subject

Disclaimer: My AppleScript isn’t great. YMMV. Use at your own risk.

-- Mail Rule - File messages & attachments
-- Created by Christian Grunenberg on Fri May 25 2012.
-- Copyright (c) 2012-2014. All rights reserved.

-- POSIX path of destination database. Global inbox is used if not specified.
property pDatabasePath : ""

-- 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 pMessageLocation to "/" & (name of theRule)
		-- set pAttachmentLocation to pMessageLocation & "/Attachments"
		tell application id "DNtp"
			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 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 "DNtp" to set message_group to create location theSubject in destination_database
					tell application id "DNtp" 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 message_group
					repeat with theAttachment in mail attachments of theMessage
						set theFile to theFolder & (name of theAttachment)
						tell theAttachment to save in theFile
						tell application id "DNtp"
							set theAttachmentRecord to import theFile to message_group
							set unread of theAttachmentRecord to (not theReadFlag)
							set URL of theAttachmentRecord to theSender
						end tell
					end repeat
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from
1 Like