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.