Running a script with an Applemail rule

I’m not sure if this is an Applescript or Applemail question but I’ll try here first. I am having a problem with a script that is run by an Applemail rule. The script basically extracts some data from email in a specific format. The script works fine as long as there is only one email to process. Where it goes afoul is when there are multiple emails. It appears that perhaps there is not a separate execution of the script for each email. Can someone confirm this either way and do I need to use a loop with ‘theSelectedMessages’ rather then assume one execution per message? Thanks.

Hi,

for processing multiple messages you need a repeat loop like this

using terms from application "Mail"
	on perform mail action with messages TheMessages for rule theRule
		tell application "Mail"
			repeat with OneMessage in TheMessages
				-- do something
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Thanks for the reply. Do I take it then that there will be only 1 execution of a rule regardless of how many emails that match that rule?

If that is the case then it leads to a second question. How do I sort the order by date for the loop? TIA.

yes

read the dates of the mails into a list and sort both lists simultaneously .
Sorting routines for two lists you can find here

Thanks. That’s what I wanted to know. However, the sorting is a bit daunting. I had no idea it would be that complicated and I’m only dealing with a handful of emails. Maybe passing it to Unix Sort would be easier. Thanks again.