Updated to Mountain Lion and Mail Script No Longer Works...

Hello -
I have DTPO, just updated from Lion to Mountain Lion and simultaneously updating from 2.5.x DTPO to latest 2.6.1 DTPO. I use Apple’s Mail client.

Until now, my workflow has always involved, using the script “Mail Rule Action Import” and configuring my rules in mail so that ALL of my mail would be copied into my DTPO global in-box. (I do this by having a rule that says “if sender IS in my contacts” send to DTPO, and another rule that says “if sender is NOT in my contacts” send to DTPO; the net result is that everything gets sent to DTPO and then I put sort the emails into whatever project files they pertain to.

Sadly, upon the above mentioned updates, emails come into Mail … but they do not go to DTPO. I see that the rules still exist (although now I have tweaked them around to experiment so not sure if they are the same as they started). But no matter how I switch the rule, nothing happens. Do I need to update the script? It is located at Library>Application Support>Devonthink Pro 2>ExtraScripts>Mail>Mail Rule Action Import, and I notice that is not exactly what others on the forum are describing…, so mabybe what I’ve been doing for the last couple years is now outdated and people set this up differently?

Any help is appreciated – this is important to my workflow, and I’ll quickly get totally buried in e-mails if I can’t auto-process them this way. Thank you!

I now see from another similar topic that a set of scripts is also at Library>ApplicationScripts>com.apple.mail>______, where mail rules end up mentioning DEVONthink, so perhaps these are in fact the scripts being harnessed by my non-working rules in this new scheme of things. I don’t know. In any event it no longer works!

If this method no longer works, has anyone come up with a good work around, to keep the mail automatically flowing into DTPO without human intervention, almost as if DTPO was the mail client-plus? That’s how I had it before.

Have you tried the DEVONthink menu item > Install Add-Ons… > Application Mail Rule Scripts and restart Mail? If it’s grayed out, you can remove and reinstall them from here.

Thanks for the response Blue Frog. I did do that – first when I was updating DTPO, and it prompted me I had it reinstall all the scripts and add-ons. Then, upon reading your suggestion just now, I went to the DTPO menu item and did so again, just in case it “didn’t take” the first time. Unfortunately, no luck.

I was able to ascertain one bit of information though: When in the Mail preferences area, when I chose to “edit” the DTPO “rule” it gives me an option to see where the operative script is in the finder, so I’ve now confirmed I’m dealing with the DTPO scripts that are in the com.apple.mail folder.

When I open the pertinent script for the rule I’m currently trying to use, the content looks pretty simple (though unfortunately I do not yet know apple script), here it is cut-and-pasted if this reveals any clue of why it might not work:

– Mail Rule - Add messages to DEVONthink Pro.
– Created by Christian Grunenberg on Mon Apr 19 2004.
– Copyright © 2004-2012. All rights reserved.

– 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
tell application “Mail”
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 incoming group
set unread of theRecord to (not theReadFlag)
end tell
end try
end repeat
end tell
end perform mail action with messages
end using terms from

Thank you for pasting the script you found – it is the same one included in the DEVONthink download and installed using the tip that Jim gave you, above. Over here, the script works. On Mountain Lion. With DEVONthink Pro Office 2.6.1.

You mentioned that you upgraded recently to Mountain Lion, and that you changed the content of the rule you were using prior to the upgrade and perhaps do not remember what the old rule was.

Maybe the problem isn’t the script (it isn’t) but something in the upgrade or the (maybe) new rule?

Since you have Pro Office, have you tried using the Mail Plugin? Or the Email Import? If either or both of them work for you, then I’d suggest that the problem lies outside of DEVONthink.

Yes, the script still works in OS 10.8 with DEVONthink Pro Office 2.6.1, but what is broken - in my experience - with OS 10.8 and Apple Mail 6 is that the emails get automatically transferred to DEVONthink Pro Office when they arrive. (With POP-accounts that is, I cannot speak for IMAP.)
Does the auto-export from Mail to DEVONthink Pro Office really work for you or do you have to manually apply the rule(s) the script is attached to to start the export to DEVONthink Pro Office?

Because ‘auto’ is what he wants:

Of course it’s auto-export over here. Isn’t that what the OP wanted?

What’s the point of a such an export/import mail rule that has to be triggered manually? If you want manual export, use the DEVONthink plugin or drag the message to DEVONthink.

Interesting, here it doesn’t.

In my case to file all recent emails and attachments directly into dedicated, different databases and dedicated groups in one go, with one shortcut.

Thanks, Korm and Berndm.

This post will be very long and detailed, so thank you to anyone with the patience to read it. I’ve looked across the forums, after searching “mail” and “mountain lion” and I can tell that I am not the only one having this issue, although people have described it in various ways, it appears likely to be the same problem. So, here are the details as best as I can provide them [and thanks again to anyone who can get through this!]

Fortunately, prior to updating DTPO and updating from Lion to Mountain Lion, I made a bootable clone on an external hard drive. Therefore, this afternoon, I booted up, from my prior system using the clone. It was sad, because my poor clone system did not know that it was only a ghost, and acted like it was still alive – reminded me of Dixie Flatline from Neuromancer or maybe Idaho from one of the later Dune books.

Anyway, Mail client clone started sucking-in mails that it had missed whilst being dead, and started faithfully shooting copies of them to DTPO, which received them in the in-box. Thus confirming that everything worked right before updating.

My observation summary observation is that – in the current system (Mountain Lion and DTPO 2.6.1) when in Mail>Preferences>Rules, and you specify RunApplescript, it will ONLY allow you to point the rule to a specific script that traces directly to a file with an .scpt file ending. Previously, in Lion and DTPO 2.5.x the rule pointed to a whole “package”, which included a script as one portion of it. So, something has changed, and whatever that “something” is, does not allow the rule to function as it did previously.

More specifically about my observation within the old clone [where the rule works], I went to Mail preferences and looked up the pertinent rules, [which allowed the emails to shoot over to DTPO automatically as they came into the Mail client]. The “Rules” interface allowed the user to specify by free hand, the location of the script [or package as it turns out] to which you wanted to call (whereas in mountain lion and new Mail, it forces you to “select” an available script.

Still in the clone, in Mail>preferences>rules, the old rules looked as follows, and pointed to the “script” as follows:

Description: [Rule 1]
If [any] of the following conditions are met:
[Sender is in my Address Book]
Perform the following actions:
[RunAppleScript][~/Library/Application Support/DEVONthink Pro 2/ExtraScripts/Mail/Mail Rule Action Import.app]

Description: [Rule 2]
If [any] of the following conditions are met:
[Sender is not in my Address Book]
Perform the following actions:
[RunAppleScript][~/Library/Application Support/DEVONthink Pro 2/ExtraScripts/Mail/Mail Rule Action Import.app]

Notice, that the location of the script is different than in the current version. [New location appears to be:Library>Application Scripts>com.apple.mail>[several script options at the bottom level].

Also different, is that in the old system, when I use finder to navigate to said “Mail Rule Action Import.app” it is not really just a script but, is a “package” that contains a script along with other stuff. Remember, this is the old version that actually worked. Looking inside the package, it looks like this: [see attached screen shot at bottom of post, please].

The script portion within the package titled “main.scpt” has the following content:

– Mail Rule Action Import to DEVONthink Pro.
– Created by Christian Grunenberg on Mon Apr 19 2004.
– Copyright © 2004-2009. All rights reserved.

– 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
tell application “Mail”
repeat with theMessage in theMessages
my importMessage(theMessage)
end repeat
end tell
end perform mail action with messages
end using terms from

on importMessage(theMessage)
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
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 incoming group
set unread of theRecord to (not theReadFlag)
end tell
end try
end tell
end importMessage

If anyone has any idea of how to solve this problem; or maybe just a comment that will enlighten me a bit more about relevant aspects of DTPO and scripting and OSX, I’d appreciate the thoughts.

Ian, are you receiving emails via POP?

korm and me had a conversation about this issue and it turned out, that korm uses IMAP. As I wrote earlier, I use POP.
When I was in your situation at the time OS 10.8 was released I was in contact with Christian Grunenberg about the issue and within weeks following like you I found quite some threads on the web, where the same or similar problems were reported.
When I remember all things right, the problem has to do with how Apple Mail 6 / Mountain Lion handles receiving emails from POP-accounts. POP doesn’t work over here and Christian confirmed this for his configuration. With IMAP all scripts should work.

From what I tested way back then with the different script showed, that:

1.) with Mountain Lion Apple Scripts are handled in a new way, which has to do with Gatekeeper, signed apps. Therefore, if you want to run a script with a rule, this script has to be compiled. I always export these scripts as .scpt. Please see this article.

2.) if you want to run the script on new messages in the moment Apple Mail receives them (for auto-export), you should let the script file those messages to a different folder (mailbox) in Apple Mail, may it be the trash or just another folder. Otherwise they will be exported again and again, causing duplicates in DEVONthink Pro Office

3.) What I do here is: I do not run the export-script on messages when incoming, I run the script on messages after they have been received, see also this discussion.
The script is also modified to meet this.

To see all emails from all accounts at once I usually look at Apple Mail only at one smart group which shows all emails excluding those from the trash, drafts and the folder, they are filed to after export (I called this folder ‘DEVONthink’, but including sent messages, so that they also get exported in one go together with new received messages.

This is the smart group:

So, when I see, new messages have arrived, I just go into Apple Mail, do CMD+A to mark them all and do CMD + ALT + L to export them to DEVONthink Pro Office.
Let’s call it semi-auto-export. This works fine for me, but as Apple Mail has some bugs more. even though the script does not work on arrival of new messages, sometimes scattered emails get auto-exported nonetheless. If I then run the script(s) I get duplicates, but as said this happens only occasionally, I delete them with the “duplicates to thrash”-script, that is supplied with DEVONthink Pro Office.

The script I use is basically the “automatically files messages & attachments”-script, that DEVONtechnologies offers, but it has some modifications to meet the issues described above and meet my needs in addition.

  • this script does not work on emails when they arrive, see 3.) above
  • this script files exported messages to a dedicated folder in Apple Mail, see 2.) above
  • this script files messages to specified groups in DtPO
  • this script files attachments to specified groups in DtPO
  • this script needs to have the path to the DEVONthink Pro Office-database to where to export to specified.

When you look into the code and search for “INSERT” you will find all parts you have to specify to make it work for you. Otherwise just use some of the modifications for your modification of your existing scripts, if you like.

This is the script:

-- Automatically file messages & attachments
-- Created by Christian Grunenberg on Fri May 25 2012.
-- Copyright (c) 2012. All rights reserved.
-- modified to not automatically work (process email after being received)
-- modified to files messages to a mailbox after export

-- POSIX path of destination database
property pDatabasePath : "INSERT path to DtPO-database including name of the database e.g. /Users/YourName/Documents/DEVONthinkPro/datenbases/xyz.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 "INSERT path to group in a DtPO-database"
		set pMessageLocation to pEmailLocation
		set pAttachmentLocation to "INSERT path to group in a DtPO-database"
		
		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 "INSERT name of mailbox in Apple Mail"
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Now some may ask,why I do it this way. I have different rules and each has the script attached, but the groups to file the messages and attachments to are different and also the target databases are different ones. This saves a lot of time filing the data later.

Berndm –

Thank you very much! You obviously spent significant time putting that answer together, and I appreciate it.

I have three e-mail accounts that feed into my Mail client, and the main two are POP. The iCloud mail is IMAP (I believe), but I don’t use that for anything except for whatever Apple sends me (though I haven’t noticed any of those notices making it into DTPO either).

It’ll be a little while before I have time to process your response thoroughly and try to implement it (this weekend though I hope). I’ll post back on here then, to report the result.

If you would/could switch them to IMAP, you’d get back your full auto-export.
(Only reason I did not switch is, that I do not have so many devices, that IMAP would be of advantage for me and because one of my main accounts would need a different provider to run it as IMAP. And as I have to enter Apple Mail anyway to export sent messages to DEVONthink Pro Office, it’s not really unproductive for me, to run the rules and scripts with quick shortcuts manually.)

Regarding your post prior to my last answer:

Yes, the new location for the scripts is ~/Library/Application Scripts/com.apple.mail/ and the scripts are .scpt (not .app):
appscripts_mail.jpg
If you place your own modified script (or the one I use) there, please remember to compile it, before you save or export it to this location. (The scripts starting with “Mail Rule …” you see in the screenshot are installed by DEVONthink Pro Office.)

Your rules within Apple Mail look fine.