Apply smart rule after email import

Hi,

Wonder how I could auto run a smart rule after import emails from Apple Mail with rule in apple mail.
Tried so much settings. Nothing worked.

Scripts (like the one used in your rule in Apple Mail) don’t trigger rules to make their result predictable. One workaround might be to use a scheduled smart rule, another one to add perform smart rule to the script.

Thanks. I’ll test it. Just beginner with Apple Script. :wink:

Which rule script do you use and what’s the name of the rule?

“Mail Rule - Add Message to Devonthink” and “Mail Rule - Add Attachments to Devonthink”. Both. Depending on Email.

And the name of the rule in DEVONthink?

Sorry. Name is “Rechnung Apple-Music”

A revision like this one should work in case of the “Mail Rule - Add message(s) to DEVONthink” script:

-- 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
						perform smart rule name "Rechnung Apple-Music" record theRecord
					end
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from
1 Like

Thank you so much. it worked, just action-step “moving” in Database didn’t work. Any idea?

A screenshot of the rule in DEVONthink would be useful.

And all actions are successful but the last one fails? That’s weird. Is anything logged to Windows > Log?

My mistake. After few imorts UUID already exists. Now it works. Thank you so much. Never seen that support before.

One more question. Because I have many rules or incoming emails to DT with Mail Plugin: I have a smart rule in DT that move all emails to group in the Database called “Emails”.
That work fine if after changing your script.
it is now possible to start an new smart rule? Or it is the same issue like before?

Is that the name of the rule or of the database?

That’s the group name. Rule name is “Emails verschieben” as seen on screenshot below.
other smart rule “Rechnung Apple Music” should observe group “Emails”. I tried “On moving” or “On Import” or “On Moving into Database” - nothing worked.

Why not use an interval event trigger, like Hourly ?

Yes.Will work fine. Just looking for a solution if I need data directly without apply rule be myself.
But if not possible I have to do.