Mail.app's rules are sending the wrong messages to AppleScript

I have the same issue as described here --> https://discussions.apple.com/thread/4820345 (this thread ended without a solution) or here --> https://stackoverflow.com/questions/48304651/mail-apps-rules-are-sending-the-wrong-messages-to-applescript



System: Mac OS High Sierra 10.13.6, Mail 11.5



The script is stored as a mail rule:

using terms from application "Mail"
	on perform mail action with messages theSelectedMessages for rule theRule
		tell application "Mail"
            set sumSelectedMessages to count of theSelectedMessages
			repeat with theMessage in theSelectedMessages
				delay 1
				set theAccount to name of account of mailbox of theMessage
				set nameSourceMailboxMail to name of mailbox of theMessage
				set IDofMessage to id of theMessage as rich text
				set theSubject to subject of theMessage
				my logToConsole("B. theAccount: " & theAccount & " nameSourceMailboxMail: " & nameSourceMailboxMail & " IDofMessage: " & IDofMessage & " theSubject: " & theSubject)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on logToConsole(txt)
	display dialog "Text: " & txt
	#set output_date to (do shell script "date '+%Y.%m.%d-%H:%M:%S'")
	#do shell script "echo " & output_date & " " & quoted form of (txt as string) & " >> ~/Desktop/mail_script_debug.txt"
end logToConsole



When manually select emails, the script run as expected.


When new emails arrived or emails in subfolders marked as new and copied to the inbox, only the last selected/marked message - sometimes even those from a subfolder - is in the buffer ="theSelectedMessages").


The content of the counter "sumSelectedMessages" is ok.


The "delay 1" (or other values) doesn´t change anything.


I tested also with a "try" condition inside the repeat loop without success (or more info).



Any hints, infos or possible debug scenarios are appreciated !

Posted on Jan 22, 2019 7:51 AM

Reply

Similar questions

12 replies

Jan 22, 2019 12:19 PM in response to BDAqua

inbox: when manually select emails and run rules, the script run as expected.

subfolder: select/mark the same mails as "unread" + move them to the inbox, the script startet automatically but got always the wrong messages.


After moving the selected messages to the inbox, Apple Mail select the next message in the subfolder automatically.

Jan 22, 2019 12:45 PM in response to Ruskes

no because the script match the defined criteria (in the "standard" rules).



The above coding is only an extraction of the actual script. The original passes the selected mails and store them incl. attachments to a DMS application & save the mail additionally in a special subfolder.



In the past, the script did not always go right. Sometimes it stored the wrong mail in the DMS.


That's why I went for troubleshooting now.


However, I did not expect the problem to be so "complex" with such a simple mail script code :-)



Jan 22, 2019 1:20 PM in response to Goulasch

I wonder if you can use ThunderBird?


https://www.thunderbird.net/en-US/


STEEL is the Scriptable Thunderbird Easy Extension Library. Similar to Firefox's FUEL, it is JavaScript library designed to help developers build extensions using terminology and interfaces that are familiar to them. like set of interfaces to facilitate Thunderbird extension development. The steelIApplication interface implements all the functions of extIApplication.


https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Thunderbird_extensions/An_overview_of_the_Thunderbird_interface


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.app's rules are sending the wrong messages to AppleScript

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