Mail.app Delete Selected Trashed Messages

Hi fellow scripters.

I have a script that isn’t doing much, and I don’t know why. I want to delete chosen messages that are in the Trash folder.

property deleteFromNames : {"Zumies", "Dancing Wombats"}

tell application "Mail"
	set counter to 0
	set trashFolder to mailbox "Trash" of account "VP"
	set trashMessages to messages of trashFolder
	repeat with aMessage in trashMessages
		set fromName to extract name from sender of aMessage
		if deleteFromNames contains fromName then
			set counter to counter + 1
			delete aMessage
		end if
	end repeat
	display dialog counter buttons {"OK"} default button 1
end tell

I’m getting a dialog with a result of 140, but they won’t delete. Not sure why. Can anybody provide some insight?

Cheers

My accounts don’t have a “Trash” mailbox. they have a “Deleted Items” mailbox.

My iCloud account has a “Deleted Items” mailbox and a mailbox that views as “Trash”, but when viewed in AppleScript its real name is “trashFolder:Deleted Messages”

Run the script below and see if the list has a mailbox name in it called “Trash”

tell application "Mail"
	set anAccount to account "VP"
	return name of mailboxes of anAccount
end tell

If the name “Trash” is not in the list, then there is no way to get to that mailbox programmatically, even tho you can see it.

Ya that’s what I was attempting when you updated your reply:

{"All Mail", "Starred", "Archive", "INBOX", "Drafts", "Sent", "Trash", "Spam"}

What kind of email account is it? (i.e. IMAP, POP3, Exchange, iCloud)

ALSO

On my iCloud account, I can delete messages on any mailbox but the Trash mailbox.
For that I have a GUI script that does this…

click menu item "In All Accounts" of menu 1 of menu item "Erase Deleted Items" of menu 1 of menu bar item "Mailbox" of menu bar 1

Protonmail IMAP. But I wouldn’t think the nature of the account should change the ability to delete emails in that folder, much like my Mail.app client allows me to.

Your script doesn’t work for me. But I see how you use the GUI to get to the deletion.

Sorry! You have to put it in a tell block

tell application "System Events"
	tell application process "Mail"
		click menu item "In All Accounts" of menu 1 of menu item "Erase Deleted Items" of menu 1 of menu bar item "Mailbox" of menu bar 1
	end tell
end tell

works for me

Here is a version that will also click the button “Erase” in the sheet dialog that appears

tell application "System Events" to tell application process "Mail"
	set mainWin to window 1 whose description is "standard window"
	set mymenu to menu 1 of menu bar item "Mailbox" of menu bar 1
	click menu item "In All Accounts" of menu 1 of menu item "Erase Deleted Items" of mymenu
	repeat with i from 30 to 0 by -1
		if exists sheet 1 of mainWin then exit repeat
		delay 0.2
	end repeat
	if i > 0 then
		click button "Erase" of (sheet 1 of mainWin whose description is "alert")
		repeat with i from 30 to 0 by -1
			if not (exists sheet 1 of mainWin) then exit repeat
			delay 0.2
		end repeat
	else
		return false
	end if
end tell
1 Like

But of course. Still didn’t work. Error on ‘click menu item’.

This worked great for me on a Mac Mini, 3.6 GHz Quad-Core Intel Core i3, running Mac OS Ventura 13.4.1.

It’s very frustrating and disappointing that plain AppleScript does not seem to delete Trash mailbox items.

Sorry, this is a year old. I don’t remember last week.

I know this is old, but IIRC, as far as Mail.app is concerned, deleting a message equates to move to Trash folder.

Therefore, the script should work on non-Trash folders, but if the message is already in the Trash, there’s nothing for Mail to do (as far as it’s concerned)

You are somewhat correct. You would have to use GUI scripting to choose the “Erase Deleted Items” menu item, as in the example script above.