Trying to get my script to delete all emails in all folders whose name contains ‘trash’, ‘delete’, or junk.
It will move files in junk folder into trash, but won’t delete trash
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
set accountsList to name of accounts
repeat with anAccount in accountsList
set anAccount to contents of anAccount --mailboxes of account anAccount --mailbox "Junk" of account anAccount
set mailboxList to mailboxes of account anAccount
set c to 1
repeat with i from 1 to count mailboxList
set flag to true
repeat with mboxName in {"deleted", "trash", "junk"}
if mboxName is in name of item i of mailboxList then
set flag to false
exit repeat
end if
end repeat
if flag then
set item i of mailboxList to item c of mailboxList
set c to c + 1
end if
end repeat
set mailboxList to items c thru -1 of mailboxList
repeat with aMailBox in mailboxList
set deleteList to messages of aMailBox
repeat with aMsg in deleteList
set aMsg to contents of aMsg
delete aMsg -- won't delete message if already in trash folder
end repeat
end repeat
end repeat
end tell