Applescript triggered by mail rule does not reference the new message

In Apple Mail I use a mail rule to trigger an Applescript for one specific sender. This script for the moment just logs the subject of the mail message. The script looks like this:


using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        set cnt to 0
        repeat with msg in theMessages
            try
                set txt to subject of msg
                my logToConsole(txt)
            on error err
                my logToConsole("Error processing message: " & err)
            end try			
        end repeat
    end perform mail action with messages
end using terms from

on logToConsole(txt)
    do shell script "/usr/bin/logger -t \"Mail script:\" " & txt's quoted form
end logToConsole

When a new message from the specified sender comes in, the script is triggered aand writes the subject to the console. So far so good.

However, the subject written to the console is the subject of the last message in the Inbox straight under the new message, not the subject of new message.

To illustrate this, lets say, my Inbox looks at the beginning like this:

[format]
From To Subject Date

Maria Leo Overview Today 6:03pm
Carl Leo Meeting Today 5:13pm

[/format]

Then the new message (from Peter) arrives:

[format]
From To Subject Date

Peter Leo Details Today 8:45pm
Maria Leo Overview Today 6:03pm
Carl Leo Meeting Today 5:13pm

[/format]

Peter is the sender I used in the Applescript mail rule, so the script is triggered on arrival, but it logs to the console the string “Overview”, not the string “Details”.

Is there a way to fix this? I obviously want to work on the new message.

For testing, you need to rearrange or remove your try statement, which obscures error reporting, and I would use a dialog for more immediate feedback.

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with msg in theMessages
			
			my display(msg's subject)
			
		end repeat
	end perform mail action with messages
end using terms from


on display(this)
	display dialog this
end

Mmmmmmmh, I tried this, but the result is the same: The wrong message is processed. :frowning:

You wrote that it’s not the first message which appear in the report.
OK.
But, are every messages listed in this report ?

I ask that because for tests I ran :

tell application "Mail"
	set theMessages to selection
	count theMessages
	
	
	repeat with msg in theMessages
		try
			set txt to subject of msg
			my logToConsole(txt)
		on error err
			my logToConsole("Error processing message: " & err)
		end try
	end repeat
end tell

on logToConsole(txt)
	do shell script "/usr/bin/logger -t \"Mail script:\" " & txt's quoted form
end logToConsole

In the selection, the messages were ordered by date receive in a given box, but in the report they are ordered differently.
#150071 report 08
#150111 report 03
#150201 report 09
#150372 report 04
#150443 report 02
#150507 report 05
#150556 report 10
#150583 report 01
#150707 report 06
#150748 report 07
The first number is the ID of the message, correctly ordered according to date received.
The second number is the index in the log report.
Honestly I don’t guess what logic is being this ordering.

So, I edited the script as :

tell application "Mail"
	set theMessages to selection
	count theMessages
	
	
	repeat with msg in theMessages
		try
			set itsID to id of msg as rich text
			set txt to subject of msg
			my logToConsole(itsID & tab & txt)
		on error err
			my logToConsole("Error processing message: " & err)
		end try
	end repeat
end tell

on logToConsole(txt)
	do shell script "/usr/bin/logger -t \"Mail script:\" " & txt's quoted form
end logToConsole

This way, the log is :
18/04/2016 11:29:50,973 Mail script:[875]: 150583 vernissage : Frédéric Ballester, lumières des songes…
18/04/2016 11:29:50,989 Mail script:[876]: 150443 invitation à la Librairie Niçoise le mardi 26 avril à 18h
18/04/2016 11:29:51,003 Mail script:[877]: 150111 Yvan, simplicité rime avec rapidité. Pour vous, 25 annonces gratuites !
18/04/2016 11:29:51,018 Mail script:[878]: 150372 Le Caravage du grenier est authentique
18/04/2016 11:29:51,033 Mail script:[879]: 150507 David Chase, le père des “Soprano”, raconté par ses proches
18/04/2016 11:29:51,048 Mail script:[880]: 150748 SPAM Accusé de réception
18/04/2016 11:29:51,063 Mail script:[881]: 150071 Regardez en exclusivité “Mauvais Souvenir”, documentaire bouleversant sur le Rwanda
18/04/2016 11:29:51,078 Mail script:[882]: 150201 Sunday Jazz Gazette - April 10, 2016
18/04/2016 11:29:51,092 Mail script:[883]: 150556 Architecture in Detail II

And I may see the date received order by looking at the message IDs.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 18 avril 2016 11:34:46

Hi. It’s possible that the trigger item has yet to be realized by the handler. In a previous thread about this,
http://macscripter.net/viewtopic.php?id=43490
Kel advised to use the rule to move the messages to another mailbox. You might experiment with that method.