AppleScript to Toggle the macOS Desktop

Here’s an AppleScript I use to quickly toggle desktop visibility for taking screenshots and recording screencasts that I thought might be useful for others.

Copy and paste the code into AppleScript Editor, or your editor of choice, compile and save. As always, scripts like this work best using FastScripts from Red Sweater Software.

on run
    tell application "System Events"
        set frontMostApp to name of the first process whose frontmost is true
    end tell
        set theDefault to ((do shell script "defaults read CreateDesktop") as integer) as boolean
    on error -- if the default value doesn't already exist, create it...
        do shell script "defaults write CreateDesktop 1"
        set theDefault to ((do shell script "defaults read CreateDesktop") as integer) as boolean
    end try
    do shell script "defaults write CreateDesktop " & (((not theDefault) as integer) as string)
    tell application "Finder" to quit
    delay 1
    tell application "Finder" to launch
    tell application frontMostApp to activate
end run

Note: This single script turns off the desktop if it’s on, and turns it on if it’s off – just to clear up the question should it need to be asked.

Rebuilding the Apple Mail Envelope Index

Some time ago, I set up my Gmail account in Apple Mail to copy over messages which were not sent to my account. I’m one of those guys who likes to have everything all in one place at my finger tips when I need it.

After copying over the messages I wanted, I completely removed the Gmail IMAP account from Mail and went about my business. Soon after, when I went searching for a specific email, I experienced the phenomenon of ghost emails in the search results.

These are messages that Mail once knew about, but no longer knows where they exist. The ghost emails were all from the since removed Gmail account.

If you’re experiencing the same problem, here’s a very simple fix to rebuild the Apple Mail envelope index.

First, quit Apple Mail if it’s running, then navigate your way to “~/Library/Mail/” (where “~” is your home folder) in the Finder. Once you’re in the Mail folder, you’ll see a file named “Envelope Index” which keeps track of where all messages are located. Rename “Envelope Index” to “Envelope Index Backup” (We don’t want to trash the file just yet).

Then, relaunch Apple Mail. You’ll be presented with a daunting “Message Import” dialog that looks like your email account was wiped out and you’re starting all over. Fear not. You’re simply rebuilding the Envelope Index at this point.

Click “Continue” to rebuild the index. Once it’s done, all those pesky phantom messages will be gone the next time you perform a search in Mail (Yay!). You can then go back to the “~/Library/Mail/” folder in the Finder and move the file you renamed to “Envelope Index Backup” to the Trash.

And there it is. Your Apple Mail Envelope index has been rebuilt. A very simple solution to a problem that has baffled some of the best Mac users.