AppleScript to purge the Trash

Tried to delete all messages in trash with this line:

tell application "Mail" to delete every message in trash mailbox

Operation goes without a mistake, but nothing happen… Messages are still in the Trash.
The same line with junk mailbox goes as intended. What’s wrong?

Big Sur, SD7

Because the delete command seems to put messages in the trash mailbox.

I think you need to “empty” the trash.

I just found this on StackOverflow:

Mail.app will then proceed according to your account settings (Mail preferences > Accounts > (your account) > Mailbox Behaviors > Trash) and move the message to its account’s Trash mailbox or else delete it immediately, depending on your settings.

Thanks @suzume , but there are no such AppleScript function empty trash mailbox, though it would be nice…

It seems really strange behavior. I want to use Trash mailbox, I like when Mail asks if I’m sure to purge the Trash, but when I’m really sure and do it intentionally, I want this action to be quick and without any additional questions. And? You just can’t do it…

As a temporary workaround I use a Keyboard Maestro macro: first action - executes simple AppleScript action purging All Junk, second action - Mail Menu Item: Mailbox - Erase Deleted Items.

Mail is really strange… I’d be happy to see Mail as a paid app, if it were the only reason why we see such a long lasting bugs, like you cannot tell to outgoing message if it was not created by AppleScript, Mail just don’t see it… What a …??? And this bug appears to be more than 8 years old!

According to the thread I referred to, the emptying depends on the account setting, but it is my understanding that you can change the setting in your script.

I understand, but there are no option to “delete immediately” in “Mailbox Behaviours”, and even if it were - it’d mean you have no Trash - all your messages are deleted immediately… Or I got something wrong?

Yes. I’m afraid expecting much of Mail and AppleScript can lead to insanity. Years ago, I was trying to workaround one problem, and by looking at the dictionary via SD’s Explorer view, I could see the property I needed appear briefly for just an instant, and then disappear.

I’ve had much better luck scripting Airmail, with the developer actually responding to requests for AppleScript improvements and fixes. But, alas, Airmail has some formatting issues, and I’m back to using Mail as my primary client now.

Yes, me too. Except I wouldn’t say I had a better luck with scripting Airmail. I think developers stopped developing Airmail and started a race to squeeze more money out of the sales until all users are gone by chain reaction.
AppleScript possibilities are very limited there: proprietary kind of message-id which is the same for all selected messages (!!!)… all the more it doesn’t work now on a mobile counterpart (if you are trying to call a message by the link from, say, calendar).

So, I’m back with Mail again… With all the lasting bugs, it is still the best overall choice “ceteris paribus”.

Maybe UI scripting ?

Thanks for the update on Airmail. Unfortunate. Email is still at the very center of my work life on a Mac. Apple has a good feature set, but it is sad that Mail is so buggy both in regular use and via scripting.

Finally I did just that - Keyboard Maestro macro, which combines “Execute AppleScript” action and “Menu” UI action.

Yes, Airmail was an interesting project. And maybe developers will revive it, but I’ve been waiting for more than 2 years - nothing except cosmetic changes… This is sad.

This is the AppleScript I use to Erase Junk Mail and Erase Deleted Items:

tell application "Mail" to activate

tell application "System Events"
	tell application process "Mail"
		-- Erase Junk Mail
		tell menu item "Erase Junk Mail" of menu "Mailbox" of menu bar item "Mailbox" of menu bar 1 to click
		tell button "Erase" of sheet 1 of window 1 to click
		-- Erase Deleted Items
		tell menu item "In All Accounts…" of menu "Erase Deleted Items" of menu item "Erase Deleted Items" of menu "Mailbox" of menu bar item "Mailbox" of menu bar 1 to click
		tell button "Erase" of sheet 1 of window 1 to click
	end tell
end tell
1 Like

Hey Folks,

This is not extensively tested but seems to work.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/07/21 16:34
# dMod: 2021/07/21 16:34 
# Appl: Mail
# Task: Delete Messages From the Trash.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Delete, @Messages, @Trash
--------------------------------------------------------

tell application "Mail"
   delete messages of mailbox "Trash" of every account
end tell

--------------------------------------------------------

Function delete just moves any message to the mailbox, which Mail considers as a trash mailbox of this account. Inside this trash mailbox script cannot delete messages…

A little more testing…

It works on my system for everything except Gmail – but then Apple Mail is well known to not play nice with Gmail.

So – to fully empty the trash you’ll have to resort to UI-Scripting or Keyboard Maestro.

-Chris

Just what I was looking for to get rid of the Junk Mail, so thank you. If you could suggest a way to have this script recur say every hour as a maintenance to delete the junk that would be great. Appreciate it if you could add a line or two to get that accomplished if it is possible.