Can't get content of mail message when script is running through mail rule

Hello👋🏻
I made AppleScript that gets content of mail message and processes it through the rest of the script. Normally, when I launching script through selected message in mail, it works, it get content of message without a problem, but when I append this script to mail rule to working automatically, it throws an error “Can’t get «class ctnt» of «class mssg» id 81583 of «class mbxp» “INBOX” of «class mact» id “DF0B1473-5168-40FB-BF42-8D597E28BD5D”.”

-- Function to logging error's
on logError(errorMessage)
	do shell script "echo " & (quoted form of errorMessage) & " >> ~/Desktop/mail_rule_error.log"
end logError

using terms from application "Mail"
	on perform mail action with messages messageToForward for rule myRule
		set firstMail to item 1 of messageToForward
		try
			set contentOfMail to get content of firstMail
		on error errMsg
			logError(errMsg)
		end try
		set everyPara to get every paragraph of contentOfMail
		
	end perform mail action with messages
end using terms from

Hi @Pietruszex. Welcome to MacScripter.

The first thing I’d try would be to include a tell application "Mail" statement. The using terms from expression is only there to help the compiler with the terminology in the following line. I think you may also need to tell Mail actually to perform the actions in the handler:

-- Function to logging error's
on logError(errorMessage)
	do shell script "echo " & (quoted form of errorMessage) & " >> ~/Desktop/mail_rule_error.log"
end logError

using terms from application "Mail"
	on perform mail action with messages messageToForward for rule myRule
		tell application "Mail"
			set firstMail to item 1 of messageToForward
			try
				set contentOfMail to get content of firstMail
			on error errMsg
				my logError(errMsg)
			end try
			set everyPara to get every paragraph of contentOfMail
		end tell
	end perform mail action with messages
end using terms from

I added tell Mail but still not working, throws same error.

Hi @Pietruszex.

I’m afraid I’ve not not able to reproduce the error. :face_with_raised_eyebrow: When I use a mail rule to run the following version of your script on an incoming message, the message’s paragraphs are read out and no error log file appears on the desktop:

-- Function to logging error's
on logError(errorMessage)
	do shell script "echo " & (quoted form of errorMessage) & " >> ~/Desktop/mail_rule_error.log"
end logError

using terms from application "Mail"
	on perform mail action with messages messageToForward for rule myRule
		tell application "Mail"
			set firstMail to item 1 of messageToForward
			try
				set contentOfMail to get content of firstMail
			on error errMsg
				my logError(errMsg)
			end try
		end tell
		set everyPara to get every paragraph of contentOfMail
		repeat with this in everyPara
			say (this as Unicode text)
		end repeat
	end perform mail action with messages
end using terms from

The error message you’re getting is interesting. It’s basically saying that the script can’t get the message’s content. But it starts with:

"Can’t get «class ctnt» of «class mssg» id 81583 …

… whereas according to Script Debugger, it should start with something like:

"Can’t get «property ctnt» of «class mssg» «constant kfrmID » 81583 …

I don’t know if this indicates an error somewhere in your script or if things are reported differently within mail rules. :thinking:

Ok, that’s strange. Are you running this on Sonoma or older system? I tried this also on Catalina and there also throws the same error.

On Ventura, the system before Sonoma. I didn’t use Mail before then. It’s Mail 16.0 on both my Venture and Sonoma systems.

It’s certainly a mystery. Presumably the error message you’re getting is being written to the file on your desktop each time and you’re deleting the file before each test to make sure you’re not just seeing the same old message every time. If so, then the message must be coming from the script, which means that the rule must be successfully invoking the script, which means you must have that set up correctly. Does the rule do anything else to the message besides running the script?

I can’t think of anything else. :confused:

Yea, I got the same error every time, so yes, rule is working (also gear wheel appear on menu bar to show that script is running).
Generally I made a script that get content of message mail and from that content getting the date and login credentials for remote sessions and adding this to calendar app. Script is working fine, running normally, when get’s content from selected message in mail but not when it’s running from the rule. I thought it might be a bug in the mail app on newest systems but when you told that it’s running for you just fine, I’m very confused at the moment. What is also strange, that getting source of the message, also throws the same error.