Another year, another new operating system, another update to the Mail Vacuuming script that I first posted in 2012. It’s an AppleScript that performs the simple task of optimizing the database that Mail.app uses for all of your messages, speeding up load and search times.
The latest version of Mail now uses a sandboxed container (in addition to a V5 bump). I personally haven’t used Mail in years, so the fixes came from @lbutlr. The gist is updated, and the code is below.
(*Speed up Mail.app by vacuuming the Envelope IndexCode from: http://www.hawkwings.net/2007/03/03/scripts-to-automate-the-mailapp-envelope-speed-trick/Originally by "pmbuko" with modifications by RomuloUpdated by Brett Terpstra 2012Updated by Mathias Törnblom 2015 to support V3 in El Capitan and still keep backwards compabilityUpdated by @lbutlr for V5 and Container folder in High Sierra and use du*)tellapplication"Mail"toquitsetos_versiontodo shell script"sw_vers -productVersion"setmail_versionto"V2"considering numeric stringsif"10.10"≤os_versionthensetmail_versionto"~/Library/Mail/V3/Maildata/Envelope\\ Index"if"10.12"<os_versionthensetmail_versionto"~/Library/Mail/V4/Maildata/Envelope\\ Index"if"10.13"≤os_versionthensetmail_versionto"~/Library/Containers/com.apple.mail/Data/Library/Mail/V5/Maildata/Envelope\\ Index"endconsideringsetsizeBeforetodo shell script"du -h "&mail_version&"| awk {'print $1'}"do shell script"/usr/bin/sqlite3 "&mail_version&" vacuum"setsizeAftertodo shell script"du -h "&mail_version&"| awk {'print $1'}"display dialog("Mail index before: "&sizeBefore&return&"Mail index after: "&sizeAfter&return&return&"Enjoy the new speed!")tellapplication"Mail"toactivate