#!/bin/sh # with props to John Gruber # http://daringfireball.net/2006/10/how_to_tell_if_an_app_is_running app_running=`osascript -e 'tell app "System Events" to count processes whose name is "Mail"'` if [ $app_running = 1 ] then # only close it, if it's open! # quit mail osascript \ -e "tell application \"Mail\"" \ -e " quit" \ -e "end tell" # need to sleep for a bit, so the mail can finish quitting sleep 1 fi #compact that index! /usr/bin/sqlite3 ~/Library/Mail/Envelope\ Index vacuum index; #reopen mail if it was open before if [ $app_running = 1 ] then open -a Mail.app fi