Mail Rule AppleScript runs, but only on old mail

Hi! I’ve got an odd thing happening with a Mail Rule I have set up, with an AppleScript action to import the email into DEVONthink. The AppleScript seems to run correctly, but it only runs when new email arrives, but it acts on only the old email that was already in the inbox, and not on the new email.

So for instance, if I send email1 that meets the rule criteria, Mail does nothing. But then I send email2 that also meets the criteria, the rule successfully runs the AppleScript on email1, but email2 stays in the Inbox, untouched. I’m trying to figure out why it triggers only on old email, and how to fix this, so that the new mail is also processed by the rule?

My Mail rule looks like this:

Screenshot 2025-05-08 at 10.36.06 AM

And my script looks like this:

-- Mail Rule - Add messages to DEVONthink
-- Created by Christian Grunenberg on Mon Apr 19 2004.
-- Copyright (c) 2004-2020. All rights reserved.
-- Modified by Mark Boszko, 2025-05-06

-- 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 "DNtp"
						set theRecord 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 incoming group of database "Database Name"
						perform smart rule trigger import event record theRecord
					end tell
				end try
				set read status of theMessage to true
				set theAccount to account of mailbox of theMessage
				set mailbox of theMessage to mailbox "Trash" of theAccount
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Again, the script seems to work fine, when it does run, but it’s not triggering on the new mail that arrives. The email just sits in the inbox until another new mail arrives, and then only the old email gets processed. Perplexing.

I have tried setting the trigger to be “To” a specific email, instead of using the email account, but that gave the same result. Is there a different trigger I should use?

I realize this is probably a Mail trigger issue, and not much to do with the script, but hoping that someone here might have an idea. Thanks!

There’s not too many places where the script could fail… that said, I’m not a DEVONThink user, and even though I installed it to try, I couldn’t get it to work - likely because my configuration doesn’t match.

That said, two things stand out to me.

One is the nested tell application blocks - your 'tell application id “DNtp” is inside the ‘tell application “Mail”’ block which, while often works, isn’t recommended. Consider closing the Mail block before invoking DEVONThink.

The second is something failing within the try block that’s getting suppressed. Either try (no pun intended) removing the try block for testing - at least that way you may see the error. Or consider adding an ‘on error’ block to catch the error and report/log it some how. At least that way you’ll see if the error is triggering and why.

1 Like

Thanks for taking a look, Camelot. Your notes are mostly things in the original script provided by DEVONthink, but it’s a good idea to clean up the code where I can, I agree.

I spoke to Apple support about it, and while I was on the phone with them, noticed if I put a display dialog in there to print the variable values, it was giving me inconsistent results. In the end, it appeared to be that the delay from displaying the dialog, and waiting for me to click OK, that was — for whatever reason — giving Mail time to “get its story straight,” and send the correct messages list to the script.

I added delay 2 at the top of the handler, and that seems to give it enough time to pass correct data to the script. Working consistently now.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		delay 2
		tell application "Mail"
			[...]
1 Like