I have spent hours messing around with this using mostly Ai. My wife’s user account on my iMac has hundreds of Trash on My Mac emails, 687 today. It keeps filling up daily. I can not get a script to work that I can use in the Calendar to auto delete Trash on My Mac emails older than 2 days or at all. All the scripts show results deleted but nothing is ever deleted from the actual Mail Box. I was able to get a script to work on the admin account, but not on a user account. It seems to me it should not be so difficult. Anyone help me here. Thank you,
To erase deleted items in all accounts, it is shift+cmd+delete on a U.S. Mac extended keyboard. To do the same thing for all Junk emails, it is option+cmd+J.
I don’t write scripts to fiddle with email and certainly would not trust coding information gleaned from AI sources. Those two keyboard shortcuts serve me just fine.
More importantly, you need to track down why you are accumulating all of those Trash on your Mac emails and potentially resolve it at the source without writing code.
I agree with @VikingOSX re: needing to determine the cause of this glut of trash emails.
Mail will resist doing this automatically. This will ( I should say may - I don’t know your OS version ) get you to a dialog confirming the intent to delete your trash emails permanently. Clicking the dialog is left undone.
tell application "Mail"
activate
set targetAcctID to id of account 2 --or "email@domain.com"
set emAddy to item 1 of (get email addresses of account 2)
tell trash mailbox
set trashMailboxes to mailboxes
repeat with thisTrashMailbox in trashMailboxes
tell thisTrashMailbox
set theTrashMailboxID to id of its account
if theTrashMailboxID is targetAcctID then
--return (a reference to it)--The trash folder you seek
set TrashMessageList to messages
if length of TrashMessageList = 0 then return {}
end if
end tell
end repeat
end tell
end tell
tell application "System Events"
tell application process "Mail"
activate
tell menu bar item "Mailbox" of menu bar 1 to click
delay 0.5
tell menu item "Erase Deleted Items" of menu "Mailbox" of menu bar 1 to click
delay 0.5
set emAddy to emAddy & "…"
tell menu item emAddy 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
delay 0.5
end tell
end tell
--Result: Dialog Box "Are you sure you want to erase deleted items in the Trash mailbox of your "email@domain.com" account?
--Erasing deleted items such as messages and mailboxes permanently deletes them. This action cannot be undone.
Thank you,
This user has created thousands of spam with here shopping hobby. I use the Spamsieve App iMac Catalina to control her email accounts, but it dumps mail into the Trash On My Mac mailbox. Originally, I tried to create a script that would only delete emails in Trash on My Mac over two days old. I could not make it work. Thank you for your response. The manual method is not practical where auto delete will keep that mailbox empty.
On Run
The script returned:
Script Error
Mail got an error: Can’t get account of mailbox “Deleted Messages”.
While I don’t know why Mail.app is refusing to tell you which account owns that mailbox, I suspect you don’t have a folder named “Deleted Messages”. That entire upper portion of the script is just confirming that there are > 0 emails needing to be deleted. Feel free to remove all the code directed at the Mail app retaining only the code directed at System Events. See if the functional part of the code is working.
Scripting email clients is not simple. Each type of account may have different properties and folders etc. To determine what you need to use in your specific instance can be made fairly simple if you have or are willing to download Script Debugger. Open its Dictionary, select the Mail app and then switch to the Exploiter Tab. Examine the email account that you want to work with and determine what it’s version of a “Deleted Messages” folder is called etc.
Here us the basic script I used. Put in a calendar event to run every 3 days, and it completely clears the Trash on My Mac email mailbox.
tell application “Mail” to activate
tell application “System Events”
tell application process “Mail”
– Target the local “On My Mac” trash item in the menu
click menu item “On My Mac…” of menu 1 of menu item “Erase Deleted Items” of menu 1 of menu bar item “Mailbox” of menu bar 1
-- Safely click the "Erase" button on the confirmation popup
repeat with i from 10 to 0 by -1
if exists sheet 1 of window 1 then
click button "Erase" of sheet 1 of window 1
exit repeat
end if
delay 0.2
end repeat
end tell
end tell
Not very fancy but it works, thanks for responding to my thread.