No applescript triggered by mail rule

I’m trying to help my wife out with her business and automate a few things, She has the latest macos sonoma.

I wanted to save email attachments from a specific sender to a folder on the desktop so i cobbled together a script from sources on these forums and when running it nothing happens. i made sure that the program mail has full disk access. after trying to debug things with my friend chatgpt i decided that for some reason applescripts being triggered by my mail rule were not running at all, so i wrote a new rule to move messages from my chosen sender to a mailbox folder and run an applescript that simple logs the event. the messages were moved, but as i suspected no log event was written, so it seems applescript file is not being executed. any advice on what i should do here to trigger the script. here is the log script i tried running:

using terms from application "Mail"
	on perform_mail_action(ruleData)
		log "Test Rule Executed"
	end perform_mail_action
end using terms from

I spent 2 hours on the phone with apple support today and he couldn’t figure out why it wouldn’t run. I gave up and switched to thunderbird. problem solved!

The Mail.app’s event handler you wrote is wrong.

perform_mail_action(ruleData)

This is wrong.

using terms from application "Mail"
	on on perform mail action with messages these_messages for rule this_rule
		display dialog "Test Rule Executed"
	end perform mail action with messages
end using terms from

Mail.app’s built-in AppleScript execute runtime does not have “log” availability.

You have to text another command for example “say” or “display dialog”.

Here is my Mail.app AppleScript sample book.

Ok, when I run your example it works fine and plays the music file that I set as test.m4a!

Unfortunately, when I try to run a script that saves my email attachments in a folder called referrals on my computer it fails. Here is the script I am using to try and save email attachments:

set attachmentsFolder to ((path to home folder as text) & "referrals") as text

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			repeat with each_message in these_messages
				repeat with theAttachment in each_message's mail attachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & ":" & originalName
					try
						save theAttachment in savePath
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from```

yes that seems to work well. I am now getting much closer to my goal of saving pdf email attachments. i am using that dialog command to output errors and i am getting AppleEvent handler failed error from my new script. any ideas what i have wrong here?

using terms from application "Mail"

	on perform mail action with messages these_messages for rule this_rule

		tell application "Mail"

			set attachmentsFolder to ((path to home folder) & "referrals")

			set the message_count to the count of these_messages

			repeat with i from 1 to the message_count

				set this_message to item i of these_messages

				set these_attachments to every mail attachment of this_message

				repeat with z from 1 to the count of these_attachments

					set this_attachment to item z of these_attachments

					try

						if MIME type of this_attachment is "application/pdf" then

							set this_name to the name of this_attachment

							-- DOWNLOAD AND SAVE IMAGE FILE

							with timeout of 1800 seconds

								save this_attachment in file ((attachmentsFolder as string) & this_name)

							end timeout

						end if

					on error errMsg

						display dialog "Error: " & errMsg

					end try

				end repeat

			end repeat

		end tell

	end perform mail action with messages

end using terms from

I added some repeats and made some minor changes to your script so that i have something that saves all attachments, but it fails when I try to include a line that tests for pdf as follows:

if MIME type of thisItem is "application/pdf" then ...

I guess that’s not something in the dictionary for mail.app. I guess I have to include that in my next series of scripts as part of folder actions.

seems others are discussing this bug, eg. https://discourse.devontechnologies.com/t/script-import-mail-attachments-filtered-by-mime-type/66282

Fredrik, I am using the script you came up with here to save attachments, but am having an issue with the way mail rules are applied, I am wondering if you can help me on this. If mail.app receives an unread email then the mail rule is triggered and the applescript you provided executes without issue and the attachement is saved to the mailRuleAttachments folder on my desktop. The problem is that if the message is already read before starting mail.app then no mail rule is triggered. For example, if it is the weekend and I go into webmail from home and read my email messages they will no longer be new and so when I eventually start mail.app the mail rule is not triggered and the attachments are not saved, unless I manually apply the rule. Is there anyway around this so that the mail rule is applied and attachments are saved regardless of whether the mail message is new and unread or read previously through webmail. I guess I can set any read messages on webmail to unread, but that’s kind of finicky solution. thanks for any advice or feedback!

hi Fredrik! so these are attachments from an email to fax service that my wife has setup. since she doesn’t have a fax machine she has an email to fax service that emails her all her faxes. I am trying to save the files, print them, then do some ocr and automatic data entry on them.

I think what I might be able to do here is to run an applescript after the computer boots that highlights all emails and manually applies mail rules. I think that would get the ones that haven’t printed to print regardless of whether the email is marked as read.