Add mail attachments does not work.

For a few years I successfully had a few Apple Mail rules automatically adding specific attachments to specific folders in DEVONthink. After the Yosemite upgrade it has mostly stopped working, although on occasions it suddenly works well for a few hours…

I adapted Christian’s original script - I have no real scripting knowledge - so no idea how to understand why it no longer works. If one of the exceptionally gifted scripting gurus could give me some suggestions I would be very grateful!

The script looks for some specific string in the attachment file name - and if it matches then adds to the database. The string is comprised of initials which are between two dots e.g. “.ab.” or “.cdg.” - is there any potential problem with that? a filename would be something like APT.cdg.INEX.Mar-15-2015

The mail rule is;

If ALL are true;

From ends with @xxx.com

Attachment type is PDF

Perform following action

Run Applescript

The script is as follows;

-- Mail Rule - Add attachments to DEVONthink Pro.
-- Created by Christian Grunenberg on Fri May 18 2012.
-- Copyright (c) 2012-2013. All rights reserved.

-- POSIX path of destination database
property pDatabasePath : "/Users/sps/Documents/Quotes.dtBase2"

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set theFolder to (POSIX path of (path to temporary items))
			repeat with theMessage in theMessages
				set theSender to the sender of theMessage
				repeat with theAttachment in mail attachments of theMessage
					try
						if (name of theAttachment) contains ".cr." then
							set theFile to theFolder & (name of theAttachment)
							tell theAttachment to save in theFile
							tell application id "com.devon-technologies.thinkpro2"
								set theGroup to get record with uuid "B65CB335-F0AC-4DE3-B0F1-C0EAA92663ED"
								set theAttachmentRecord to import theFile to theGroup
								set URL of theAttachmentRecord to theSender
							end tell
							
						else if (name of theAttachment) contains ".re." then
							set theFile to theFolder & (name of theAttachment)
							tell theAttachment to save in theFile
							tell application id "com.devon-technologies.thinkpro2"
								
								set theGroup to get record with uuid "B65CB335-F0AC-4DE3-B0F1-C0EAA92663ED"
								set theAttachmentRecord to import theFile to theGroup
								set URL of theAttachmentRecord to theSender
							end tell
							
						else if (name of theAttachment) contains ".cdg." then
							set theFile to theFolder & (name of theAttachment)
							tell theAttachment to save in theFile
							tell application id "com.devon-technologies.thinkpro2"
								
								set theGroup to get record with uuid "B65CB335-F0AC-4DE3-B0F1-C0EAA92663ED"
								set theAttachmentRecord to import theFile to theGroup
								set URL of theAttachmentRecord to theSender
							end tell
							
						else if (name of theAttachment) contains ".ab." then
							set theFile to theFolder & (name of theAttachment)
							tell theAttachment to save in theFile
							tell application id "com.devon-technologies.thinkpro2"
								
								set theGroup to get record with uuid "B65CB335-F0AC-4DE3-B0F1-C0EAA92663ED"
								set theAttachmentRecord to import theFile to theGroup
								set URL of theAttachmentRecord to theSender
							end tell
							
						end if
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Most likely the script “fails” because the rule does not execute 100% of the time. This is Yosemite Mail flakiness.

However, I’d suggest you use:


tell application id "com.devon-technologies.thinkpro2"
      set  theGroup to create location "/Path/To/Destination" 
      set theAttachmentRecord to import theFile to theGroup
      set URL of theAttachmentRecord to theSender
end tell

rather than


tell application id "com.devon-technologies.thinkpro2"
       set theGroup to get record with uuid "B65CB335-F0AC-4DE3-B0F1-C0EAA92663ED"
       set theAttachmentRecord to import theFile to theGroup
       set URL of theAttachmentRecord to theSender
end tell

which contains some deprecated syntax, and a hard-coded UUID-based destination might silently break someday and is difficult to trace when investigating code.

The “create location” code I posted will not create duplicate locations if the “/Path/To/Location” already exists.

Thanks Korm! I will give this a try.