Mail rule can delete wrong message

This is slightly OT; I use a mail rule which detects a certain mail quite specifically, and triggers a script (see below). That script assists me in downloading the information the mail was pointing me to, and Hazel then sends that data to DT, where it is further processed. So far, so good. Today, I restarted my Mac. On starting Mail, three new messages were loaded. The second of those messages was the one which triggers said rule (see below). That rule then deleted the first of the new messages (which doesn’t conform to any of the criteria in the rule), rather than the message which actually triggered the rule. Can anybody explain?

The rule:

The script:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
				set c to 0
				repeat with theMessage in theMessages
					if c = 0 then set theResponse to display alert "Want me to do stuff?" as informational buttons {"Cancel", "Yes"} default button "Yes"
					if button returned of theResponse is "Yes" then
						if c = 0 then
							# do stuff
						end if
						set c to 1
						set read status of theMessage to true
						delete theMessage
					end if
				end repeat
			on error error_message number error_number
				if the error_number is not -128 then display alert "Mail" message error_message as warning
			end try
		end tell
	end perform mail action with messages
end using terms from

Cheers :slight_smile:

The first explanation coming to my mind is that Mail’s AppleScript & rule support is unfortunately not always reliable, especially in case of IMAP accounts. But of course it’s unclear what # do stuff actually does or which alert button was clicked.

# do stuff tells safari to open a website; it contains no instructions for Mail. Yes was the response to the dialog.

I am using an IMAP account, and I have previously stumbled over issues which I couldn’t explain… I guess I’ll add a further if/then containing the sender or some other prominent detail of the mail, so at least I don’t get the wrong message deleted. Impressive stuff :see_no_evil: Thanks for your feedback Criss :slight_smile:

Criss is correct on the reliability of Mail rules. They can be useful but produce unexpected results at times. I have one running with very explicit criteria to match subject lines and it still ends up triggering on some non-matching emails.

In general I only seem to get unreliable results when my Mac wakes and then Mail gets several mails at once. I don’t think I’ve had trouble when they arrive whilst the Mac is awake. That would suggest that if there were any way to delay the execution of the rules, the problem would probably be solved. It seems to me that whilst the rules apparently run top to bottom, execution of a script does not delay the execution of the next rule; so I can’t delay that way. I also wondered whether Mail has the equivalent of perform smart rule, so that automatic execution of the smart rules could be deactivated and the rules triggered instead by a script with a delay. But no.

Yeah, Apple should spend a bit of time working out the kinks with mail rules; or so I wish.