Vacuuming on El Capitan

[Tweet : nvALT]

I’m not using on OS X these days (opted for MailMate), but I received an email from Mathias Törnblom updating the database vacuuming trick I’d posted previously. It makes the script work for Mail on El Capitan, and maintains backward compatibility.

The code below will quit Mail, vacuum the SQLite index, then re-open Mail. On a large email database that hasn’t been optimized for a while, this can provide significant improvements in responsiveness and speed. To use the script, copy and paste it into a new document in Script Editor (make sure the language is set to AppleScript), and save it as a Script (scpt) file. Then you can run it via a launcher (LaunchBar, Alfred, etc.) or via the Scripts menu.

Speed up by vacuuming the Envelope Index
Code from:
Originally by "pmbuko" with modifications by Romulo
Updated by Brett Terpstra 2012
Updated by Mathias Törnblom 2015 to support V3 in El Capitan and still keep backwards compability

tell application "Mail" to quit
set os_version to do shell script "sw_vers -productVersion"
set mail_version to "V2"
considering numeric strings
    if "10.10" <= os_version then set mail_version to "V3"
    if "10.12" < os_version then set mail_version to "V4"
end considering

set sizeBefore to do shell script "ls -lnah ~/Library/Mail/" & mail_version & "/MailData | grep -E 'Envelope Index$' | awk {'print $5'}"
do shell script "/usr/bin/sqlite3 ~/Library/Mail/" & mail_version & "/MailData/Envelope\\ Index vacuum"

set sizeAfter to do shell script "ls -lnah ~/Library/Mail/" & mail_version & "/MailData | grep -E 'Envelope Index$' | awk {'print $5'}"

display dialog ("Mail index before: " & sizeBefore & return & "Mail index after: " & sizeAfter & return & return & "Enjoy the new speed!")

tell application "Mail" to activate