Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active December 8, 2023 22:00
Show Gist options
  • Save ttscoff/3728277 to your computer and use it in GitHub Desktop.
Save ttscoff/3728277 to your computer and use it in GitHub Desktop.
Speed up Mail.app
(*
Speed up Mail.app by vacuuming the Envelope Index
Code from: http://www.hawkwings.net/2007/03/03/scripts-to-automate-the-mailapp-envelope-speed-trick/
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
Updated by @lbutlr for V5 and Container folder in High Sierra and use du
*)
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 "~/Library/Mail/V3/Maildata/Envelope\\ Index"
if "10.12" < os_version then set mail_version to "~/Library/Mail/V4/Maildata/Envelope\\ Index"
if "10.13" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V5/Maildata/Envelope\\ Index"
end considering
set sizeBefore to do shell script "du -h " & mail_version & "| awk {'print $1'}"
do shell script "/usr/bin/sqlite3 " & mail_version & " vacuum"
set sizeAfter to do 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!")
tell application "Mail" to activate
@clmac
Copy link

clmac commented Oct 27, 2017

This script is not compatible with oldest version (before 10.10). To allow this, you need to change the line set mail_version to "V2" by set mail_version to "~/Library/Mail/V2/Maildata/Envelope\\ Index"

@Cyrkhan
Copy link

Cyrkhan commented Oct 3, 2018

It seems the script does not work on Mac OS 10.14 Mojave, but I don't understand why.
I added the line if "10.14" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V6/MailData/Envelope\\ Index"

but running the script gives the error :
Error: unable to open database \"/Users/MyName/Library/Containers/com.apple.mail/Data/Library/Mail/V6/MailData/Envelope Index\": unable to open database file" number 1

However, the path seems correct.

Setting "~/Library/Mail/V6/Maildata/Envelope\\ Index" instead of "/Users/MyName/Library/Containers/com.apple.mail/Data/Library/Mail/V6/MailData/Envelope Index\" gives the same error.

@akaman
Copy link

akaman commented Nov 9, 2018

I'm seeing the same issue on Mojave. I haven't found a solution yet.

@Lapsus
Copy link

Lapsus commented Dec 5, 2018

This happens because of security measures. Open System Preferences -> Security & Privacy -> Privacy tab and grant "Full Disk Access" to Terminal.app or the automator, where ever you run the applescript.

Dont forget to revoke permissions after you finished reorganizing the index.

@kildare0674
Copy link

I have ab old version of the script.
capture d ecran 2018-12-21 a 16 36 56

I try to update following all the differents steps but I had always the same error (same as the others users)

Recently I tried this:
capture d ecran 2018-12-21 a 16 37 17
I only changed the version number to "V6". I tried to use the script and:
capture d ecran 2018-12-21 a 16 38 48
It ssems to work in my case

@fgardavaud
Copy link

The method of @Lapsus work for me on Mojave macOS version

@aiBR3
Copy link

aiBR3 commented Nov 28, 2022

The updated code should be (up to macOS 13 Ventura):

(*
Speed up Mail.app by vacuuming the Envelope Index
Code from: http://www.hawkwings.net/2007/03/03/scripts-to-automate-the-mailapp-envelope-speed-trick/
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
Updated by @lbutlr for V5 and Container folder in High Sierra and use du
https://gist.github.com/ttscoff/3728277
*)

tell application "Mail" to quit
set os_version to do shell script "sw_vers -productVersion"
set mail_version to "~/Library/Mail/V2/Maildata/Envelope\\ Index"
considering numeric strings
	if "10.10" ≤ os_version then set mail_version to "~/Library/Mail/V3/Maildata/Envelope\\ Index"
	if "10.12" < os_version then set mail_version to "~/Library/Mail/V4/Maildata/Envelope\\ Index"
	if "10.13" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V5/Maildata/Envelope\\ Index"
	if "10.14" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V6/MailData/Envelope\\ Index"
	if "10.15" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V7/MailData/Envelope\\ Index"
	if "11" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V8/MailData/Envelope\\ Index"
	if "12" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V9/MailData/Envelope\\ Index"
	if "13" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V10/MailData/Envelope\\ Index"
end considering

set sizeBefore to do shell script "du -h " & mail_version & "| awk {'print $1'}"
do shell script "/usr/bin/sqlite3 " & mail_version & " vacuum"
set sizeAfter to do 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!")
tell application "Mail" to activate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment