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 !