Delete every message containing an "@aol.com" recipient?

For the past several months, I’ve been receiving spam wherein the recipients list will contain an “@aol.com” email address. This got me thinking about how I can automatically, permanently delete known Junk. This script is returning a long, “Can’t get {message id…” error. Can someone point out/fix my error?

tell application "Mail"
	set JunkBox to mailbox "Junk" of account "Home"
	set TrashBox to mailbox "Trash" of account "Home"
	set EveryMessage to every message of JunkBox
	repeat with currentMessage in (EveryMessage whose address of any recipient contains "@aol.com")
		set mailbox of currentMessage to TrashBox
	end repeat
end tell

Interesting… some instead of every.

But, now I have a new error: “Mail got an error: Container specified was an empty list.” And the AppleScript result stated: “error “Mail got an error: Container specified was an empty list.” number -1730 from some recipient”

I confirmed that there is, indeed, at least one email in that mailbox that is to an “@aol.com” address.

What returns this code on your Mail.app? :


tell application "Mail"
	tell account "Home" to set {JunkBox, TrashBox} to {mailbox "Junk", mailbox "Trash"}
	set theRecipients to every recipient of every message of JunkBox
end tell

This is returned:

{{to recipient 1 of message id 5513 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5512 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5511 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5557 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5557 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5556 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5556 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5508 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5507 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5555 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5555 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5554 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5554 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5504 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5553 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5553 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5502 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5552 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5552 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5500 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5499 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5498 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5551 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5551 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5496 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5550 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5550 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5494 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5493 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5492 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5491 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5490 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5549 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5549 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5488 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5548 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5548 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5547 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5547 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5546 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5546 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5484 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5483 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5482 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {}, {}, {}, {to recipient 1 of message id 5542 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5542 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {}, {to recipient 1 of message id 5540 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5540 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5475 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5539 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5539 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5473 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5472 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5471 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5538 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”, cc recipient 1 of message id 5538 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}, {to recipient 1 of message id 5469 of mailbox “Junk” of account id “7FEB47D0-54F4-4BBA-969F-71DAFEBFAFE4” of application “Mail”}}

Hi, again.

My suggestion with some keyword was wrong. So, I deleted it. As I see now, at least 1 repeat loop is needed here:


tell application "Mail"
	tell account "Home" to set {JunkBox, TrashBox} to {mailbox "Junk", mailbox "Trash"}
	repeat with currentMessage in (every message of JunkBox)
		if not ((every recipient of currentMessage whose address contains "@aol.com") is {}) then ¬
			set mailbox of currentMessage to TrashBox
	end repeat
end tell

Thank you. This is working better. But, I was receiving errors from the loop about not being able to find an item number (always a different number).

When I merged my original script with yours, the target messages were deleted and I received no index/item errors. Here is the working script:

tell application "Mail"
	set JunkBox to mailbox "Junk" of account "Home"
	set TrashBox to mailbox "Trash" of account "Home"
	set EveryMessage to every message of JunkBox
	repeat with currentMessage in EveryMessage
		if not ((every recipient of currentMessage whose address contains "@aol.com") is {}) then ¬
			set mailbox of currentMessage to TrashBox
	end repeat
end tell