Okay, so I know this is a tricky issue as OS X's Mail seemingly lacks the ability to automatically delete or archive old messages, even though it has the necessary date-based rules to do so.
As such, I'm currently using a smart folder to find all old messages (except in mailboxes I prefer to keep) so that I can periodically delete them by opening the smart folder, selecting everything and hitting delete.
This is a bit more convenient, but I'd still prefer the process to be automatic. Is there any way that I could have Mail, an Automator script, Applescript or something similar find old messages with some criteria (or use my smart mailbox) and delete them, so that I can schedule the process to run automatically?
Best Answer
Okay, so I've found a solution using AppleScript, it's a fairly big script but it's not actually all that complicated; it simply loops through the mailboxes of every count, skipping any specified in a list, and looks for read, undeleted messages older than the specified number of days.
The script deletes messages by locating an account's trash folder (if it has one) and moving them, which is a very slow process if it has a lot of e-mails to move initially, however it prevents the need to rescan. It will only use the regular delete option if it can't determine the trash folder (i.e - didn't find one from the names in trashMailboxes), while this is faster it may result in messages being reprocessed if the script is run frequently (e.g - daily).
To configure the script's setting you can change
defaultExpiryDays
to the number of days of e-mail to keep, everything older and read is deleted. ThedryRun
setting is set initially totrue
, which means the script will report matches but won't actually delete or move anything, once you're happy the script is matching the e-mails you expect it to then you can set thisfalse
.The
trashMailboxes
lists the mailbox names that the script will move e-mail to if found for an account. TheignoreTheseMailboxes
list contains mailboxes that shouldn't be processed, and covers most common ones, including folders that Mail's normal mailbox behaviours should cover already.The script can be run by anything that can trigger an AppleScript, such as iCal, in which case it will use its defaults. It can also be triggered via a shell script or
launchd
using a command such asosascript /path/to/script.scpt
, which will likewise use the defaults, or you can specify additional arguments in which case the first is the number of days to keep, and any further arguments are additional mailboxes to skip, for exampleosascript /path/to/script.scpt 14 foo bar
will keep the last two weeks worth of read e-mails, and will skip any mailboxes named "foo" or "bar" in addition to the defaults.If you want to automate the process with
launchd
then you can create a file under~/Library/LaunchAgents
with the extension.plist
, and contents such as:Which will keep 45 days worth of e-mails, and will run every Sunday at 1:15pm (remember to set the path to your script under program arguments!).
Once saved the launch agent will then load either next time your shutdown/restart, or you can run
launchctl load ~/Library/LaunchAgents/agent.plist
with the correct name for your plist file.