Mail rule Run AppleScript errors when accessing message properties

I'm trying to write a Mail rule that uses AppleScript to save a message's sender, subject, and content to a file.


To test, I can manually invoke the AppleScript from Mail on a single message with Message > Apply Rules, but it encounters errors when trying to access message properties, with this output being written to the file:

Can’t get «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68".
Can’t make «class subj» of «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68" into type Unicode text.
Can’t make every character of «class ctnt» of «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68" into type Unicode text.


The AppleScript is based on the one created within ScriptEditor using File > New from Template > Mail > Mail Rule Action.scptd.


I've modified it as follows to try to debug it by running it within the ScriptEditor.

using terms from application "Mail"
	on run
		tell application "Mail"
			set these_messages to selected messages of first message viewer
			set the_file to (((path to desktop folder) as string) & "mailtest.txt")
			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
				my trace("message " & i)
				my save_message(this_message, the_file)
			end repeat
		end tell
	end run
	
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			set the_rule_name to name of this_rule
			set the_file to (((path to desktop folder) as string) & the_rule_name & ".txt")
			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
				my trace("message " & i)
				my save_message(this_message, the_file)
			end repeat
		end tell
	end perform mail action with messages
	
	to save_message(this_message, the_file)
		-- GET THE SENDER OF THIS MESSAGE
		try
			set this_sender to the sender of this_message
			if this_sender is "" then error
		on error errStr number errorNumber
			set this_sender to errStr --"NO SENDER"
		end try
		my trace("sender " & this_sender)
		
		-- GET SUBJECT OF MESSAGE
		try
			set this_subject to (subject of this_message) as Unicode text
			if this_subject is "" then error
		on error errStr number errorNumber
			set this_subject to errStr --"NO SUBJECT"
		end try
		my trace(this_subject)
		
		-- GET CONTENT OF MESSAGE
		try
			set this_content to (every character of content of this_message) as Unicode text
			if this_content is in {"", "?"} then error
		on error errStr number errorNumber
			set this_content to errStr --"NO CONTENT"
		end try
		my trace(this_content)
		
		my writeTextToFile(this_sender & return & this_subject & return & this_content & return, the_file, false)
	end save_message
end using terms from

on trace(theText)
	-- say theText -- for debugging when run as Mail rule
	log theText -- for debugging when run inside Script Editor
end trace

-- writeTextToFile (not shown) is from https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html#//apple_ref/doc/uid/TP40016239-CH58-SW1


If I run it manually within ScriptEditor it works correctly. The errors above occur, however, when it's run as a Mail rule.


Any ideas? (I'm also curious where logs go when run as a Mail rule)


Thanks,


Peter


iMac 21.5-inch 2019 Big Sur 11.1

ScriptEditor 2.11 (225)

iMac 21.5″, macOS 11.1

Posted on Dec 27, 2020 10:42 AM

Reply

Similar questions

3 replies

Dec 28, 2020 7:57 AM in response to pdanielsen

If it runs under Script Editor, then it is likely one of the Privacy restrictions in Big Sur restricting access to your data from the spawned process that runs the script.

This article gives a basic rundown of what I am talking about, but i don't know what app to choose and what specific permission to give it. https://www.wikihow.com/Give-System-Permissions-for-Apps-on-MacOS-Catalina


Dec 31, 2020 9:36 AM in response to Barney-15E

Thanks for the suggestion. I tried giving Full Disk Access to Script Editor, but that made no difference.


Here are some scenarios that I've tried:

  1. If I manually Apply Rules when the message is in the Inbox and no other rules apply, it succeeds (the data is saved to the file).
  2. If I manually Apply Rules on the message when it's in a mailbox other than the Inbox and no other rules apply, the script fails (with the above errors and nothing is written to the file).
  3. If I manually Apply Rules when the message is in the Inbox and the rule is also supposed to move the message to another mailbox, it fails. This is true regardless if the move action is in the same rule or in another one that matches.
  4. If the rule is applied automatically by Mail.app on an incoming message, it succeeds (the data is saved and the message is moved).


Scenario 4 is what I want to happen automatically, so it's good that that works.


What's puzzling is why it won't work with a manual Message > Apply Rules? I'd like to be able to do that on messages that have already been received.


BTW, the account that receives the incoming message and that contains the destination mailbox is iCloud.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Mail rule Run AppleScript errors when accessing message properties

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.