Combine AND & OR statements in Mail Rules

I wanted to delete mail from a select group of senders that is older than 1 day old. I tried to set a Rule in Mail that combined “From is equal to” several hundred addresses with “Date Received Is Greater Than 1 Days Old”. However I don’t think you can combine the OR for the addresses with an AND for the Greater Than 1 Days Old. I wrote this script last week and have had mixed results. Sometimes it seems like it does not activate when mail is launched and messages that meet the criteria are left in my inbox. If I reapply the rule it then kicks in and functions as intended. I am using an imap account from Gmail. Any Ideas? Rather than have the script test the recipients and have mail check the date received, should I switch it?

property targetEmails : {"Logitech VIP Store <>", "Disney Store <>", "Orvis <>"}

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
			tell me to process(theMessages)
		on error errMsg number errNum
			tell me
				display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
			end tell
		end try
	end perform mail action with messages
end using terms from

on process(myMessages)
	script p
		property theMessages : myMessages
	end script
	tell application "Mail"
		repeat with aMessage in p's theMessages
			if aMessage's sender is in targetEmails then delete aMessage
		end repeat
	end tell
end process

Hi. It’s easy to have a conflict with another Rule, since you can’t see all their criteria concurrently, but Mail Rules can also just have inexplicably flaky operation, just like Folder Actions. You might experiment with getting the Mail Rule to check against the sender being in a group you create. The most reliable method I’ve found is to have only one Rule which triggers upon sender not being in my previous recipients and not being in my Address Book. AS Handlers can meet other conditions.

The on perform mail action handler is only necessary if you want to use the Rule’s prefiltered list. Try this:

tell application "Mail"
	delete (inbox's messages whose sender is "Logitech VIP Store <>" or sender is "Disney Store <>" and date received comes after ((current date) - 29 * days) and date received comes before ((current date) - 1 * days))
end tell

So if I ignore the list generated from the rule that might solve it.

Rather than repeat "or sender is: 300 times, I would like to reference a list. However, this generates an error:

tell application "Mail"
	return inbox's messages whose sender is in targetEmails
end tell

I think the reason that doesn’t work en masse is because the sender is actually two things”a name and an address. If you have that many senders to evaluate, they probably should be part of an Address Book group, which you can target in a Rule.

This will create your group and add the senders:

set senderList to {"Logitech VIP Store <>", "Disney Store <>", "Orvis <>"} --don't include duplicate addresses!

set convertedList to {}
tell application "Mail"
	repeat with counter from 1 to count senderList
		set convertedList's end to {extract name from senderList's item counter, extract address from senderList's item counter}
	end repeat
	my notate(convertedList)
end tell

to notate(convertedList)
	tell application "Address Book"
		tell (make group with properties {name:"Needs Managing"}) to repeat with x from 1 to count convertedList
			make new email at (make new person with properties {organization:convertedList's item x's item 1, company:true})'s end with properties {label:"Work", value:convertedList's item x's item 2}
		end repeat
		save addressbook--update
	end tell
end notate

Thank you “General Obfuscation” :wink: for the handler. Now I won’t have to manually recreate the list as a group in address book. Also as you suggested, I will be able to skip the Run AppleScript portion, as the rule will become If Sender is member of Group AND the two date requirements.