Hi folks,
NOTE: This is now a free drag and drop application that that provides options to clear cache, cookies, history etc each time Safari quits. No need for users to deal with code or scripts. Ive posted it at sourceforge. Feel free to download and enjoy!
https://sourceforge.net/projects/safaricleaner/
ORIGINAL POST BELOW:
It drives me nuts that Safari doesnt let you clear cache and cookies on quit, so I wrote a little script to do that. It also clears “local storage”, history, top sites, and has an option to turn Private Browsing on automatically on launch if you want that. Im posting it mostly to share, and any improvements would be great. Im on 10.7.3 using Safari 5.1.3. You can adjust what actions are taken or not taken easily by commenting or uncommenting try blocks in the “clear_all” handler.
The way it works is you’ll have a launch agent watching the Safari preference for changes (which happens every time Safari is launched). If Safari is launched and the Applescript isn’t running yet, the applescript will launch and run alongside Safari. When you quit either Safari or Safari_Helper, both apps quit, and all cookies, cache and local storage are cleared.
To create the launch agent, use your favorite text editor (don’t use applescript editor. I use TextWrangler, its free and great) and save the following code as a text file named “com.safarihelper.launchd.plist” . Place the file in ~/Library/LaunchAgents. Please note that you must replace with your actual user name in the 4th to last line:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>com.safarihelper.launchd</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>if ! ps aux | grep '/Applications/Safari_Helper.app'| grep -v grep && ps aux | grep '/Applications/Safari.app'| grep -v grep; then nohup /Applications/Safari_Helper.app/Contents/MacOS/applet; exit; fi</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>WatchPaths</key>
<array>
<string>/Users/<YOURUSERNAME>/Library/Preferences/com.apple.Safari.plist</string>
</array>
</dict>
</plist>
The file should have 644 permissions and be owned by your user with group staff (which is default normally). If your file has different permissions and you’re not familiar with how to set it, run the following commands in Terminal:
sudo chmod 644 ~/Library/LaunchAgents/com.safarihelper.launchd.plist
sudo chown :staff ~/Library/LaunchAgents/com.safarihelper.launchd.plist
Now for the applescript: Save the following code as a stay open application named Safari_Helper and place it in the Applications folder. Thats all there is to it. Enjoy!
tell application "Safari" to activate
--if you want private browsing automatically enabled, set "Private Browsing" command to cmd+opt+p in Keyboard system preferences, check "Enable access for assistive device" in the "Universal Access" system preference, then uncomment the next two lines.
--delay 1
--tell application "System Events" to keystroke "p" using {command down, option down}
on quit
if isRunning("Safari") is true then
tell application "Safari" to quit
end if
clear_all()
continue quit
end quit
on idle
if isRunning("Safari") is false then
quit
end if
return 5
end idle
on isRunning(appName)
tell application "System Events" to (name of processes) contains "Safari"
end isRunning
on clear_all()
set myName to do shell script "whoami"
try
do shell script "launchctl unload /System/Library/LaunchAgents/com.apple.cookied.plist"
end try
try
do shell script "rm /Users/" & myName & "/Library/Cookies/Cookies.binarycookies"
end try
try
do shell script "launchctl load /System/Library/LaunchAgents/com.apple.cookied.plist"
end try
try
do shell script "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CFNetwork.framework/Versions/A/Support/cookied"
end try
try
do shell script "rm /Users/" & myName & "/Library/Caches/com.apple.Safari/Cache.db"
end try
try
do shell script "rm /Users/" & myName & "/Library/Safari/History.plist"
end try
try
do shell script "rm /Users/" & myName & "/Library/Safari/TopSites.plist"
end try
try
do shell script "rm /Users/" & myName & "/Library/Safari/LocalStorage/*"
end try
end clear_all