Performs the vacuum command on Apple Mail's underlying SQLite Envelope Index database
I also took a few minutes to document the script and provide some information about Launchd and how to manage LaunchAgents and Services.
The download package has three files: VacuumMail, the launch agent (which is just a .plist file), and a page of documentation.
Performs the "vacuum" command on Apple Mail's underlying SQLite Envelope Index database. This must not be done while Mail is running, so this scriptlet first checks to see if Mail is running and if so, quits Mail.
VacuumMail first notifies the user that it will be shutting Mail down, giving the user an opportunity to cancel the vacuum operation. This dialog window will close automatically, quit Mail, and commence vacuuming in 8 seconds if the user doesn't click on the "Cancel" button.
VacuumMail is designed to be incorporated into an automated maintenance routine and run at regular intervals. According to the SQLite documentation, the vacuum command does a more thorough optimization of Mail's database than the "autovacuum" setting that's supported in Mac OS X beginning with version 10.4.8.
Alternatively, you can launch VacuumMail interactively if you need to do a manual run. I keep VacuumMail in my Finder toolbar, so it's handy to launch.
If Mail was running when the process begins, VacuumMail will restart Mail when it's finished. The vacuum process can take several minutes if your Envelope Index is very large, and in this first version VacuumMail doesn't display any kind of progress indicator. However, when it's finished, VacuumMail will display a dialog showing the size of your Envelope Index file before the latest vacuuming as well as afterwards.
This dialog will close after a few seconds if you don't close it manually.
VacuumMail can be installed anywhere on your system and expects Mail and your ~/Library/Mail folder to be at that location. I did modify the shell commands so they'll work with symbolic links, in case you, like me, store your Mail folder on a different partition but have it symbolically linked to your Library folder.
Using the VacuumMail LaunchAgent
The download VacuumMail package also includes a LaunchAgent for launchd that you can use to automate the running of VacuumMail. This file, org.musingsfrommars.vacuumMail.plist, should be installed in your ~/Library/LaunchAgents folder. You may need to create the LaunchAgents folder if you don't already have one.
This agent assumes that you will put VacuumMail in your /Applications/Utilities/ folder. If you want to put it somewhere else, you'll need to edit the .plist file� either using the terrific, open source launchd editor Lingon, or in your favorite .plist editor. (I recommend one called PrefSetter, a free .plist editor that's a big step up from the tool Apple distributes with its Developer Tools package.)
This agent will run VacuumMail every Tuesday afternoon at 1:00pm. I wanted to vacuum my mail weekly, and that time was chosen somewhat arbitrarily. If you want a different day and time, or a different time interval, it's easy enough to change that behavior using Lingon. The other launchd option this agent utilizes is its ability to run in low priority mode. The launch agent runs with a "nice" priority setting of 15, so it will readily yield processor privileges to other applications on your system when it runs.