Homebrew Safari [Updated]

by Francois Joseph de Kermadec

For those of you who were too busy petting their Meerkats to look at their RSS aggregators yesterday — OK, it's really an O'Reilly insider joke here —, let me say that our editor, Derrick Story, published a blog on Safari and his personal experiences with the application.



This got me thinking about our beloved browser, currently battling in my Dock for its status of default browser — a status I change more often than socks, for testing purposes and just the kicks I get from using pop-up menus.



It turns out there is one feature out there I would really, really love to see added and its notable absence from all browsers out there is something I can't really understand. Those of us who routinely develop web sites and applications know how important it is to clear (1) caches, (2) cookies, (3) favicons and (4) history between every test lest we want to run into inexplicable behavior — and this with every browser.



The problem is that there is no way to do that that does not involve a clicking and dialog-dismissing extravaganza. Fun, fun… We have a reset Safari menu but it also clears Keychain auto-fills and that is a sanity-threatening proposition — ever worked on beta sites with 2 layers of (purely useless because non-encrypted) password protection? Using private browsing might be a workaround but it does not provide the same level of confidence-inducing foamy scrubbing I need to perform about 50 times a day.



So, what is a poor little FJ to do? Whip up AppleScript, that is! Thanks to the magic of "do shell script" commands and "rm" handlers, I now have a "Reset Safari" option that just fits my needs.



I have never loved my Scripts menu as much! If the people in Cupertino who came up with the idea could send me their address, I would gladly express a box of French cookies to them! (Oh, the same applies to the WebKit and Safari teams, by the way, who are doing a superb work with the browser, even if the very little specific feature I have in mind is not available.)



Update: I have added, as requested, the script below. Please consider it uses "rm" commands and is only in alpha stage -- it works on my little machine but I did not test on a larger scale. All disclaimers apply: use at your own risk and, please, backup your data! As they say, it comes with no warranty whatsoever, expressed or implied.



try
tell application "Finder"
-- Let's get the name of the current user
set myUserName to (do shell script "whoami")

-- Is Safari running?
set safariIsRunning to false
if (do shell script "ps -U " & myUserName) contains "Safari.app/Contents/MacOS/Safari" then set safariIsRunning to true

-- Let's quit Safari or the cleaning will not take effect
if safariIsRunning then tell application "Safari" to quit

-- Let's delete the application's cookies
try
set deleteCookies to 1
do shell script "rm -r /Users/" & myUserName & "/Library/Cookies/"
on error
set deleteCookies to 0
end try

-- Let's delete the application's caches
try
set deleteCaches to 1
do shell script "rm -r /Users/" & myUserName & "/Library/Caches/Safari/"
on error
set deleteCaches to 0
end try

-- Let's delete the application's icons
try
set deleteIcons to 1
do shell script "rm -r /Users/" & myUserName & "/Library/Safari/Icons/"
on error
set deleteIcons to 0
end try

-- Let's delete the application's history
try
set deleteHistory to 1
do shell script "rm -r /Users/" & myUserName & "/Library/Safari/History.plist"
on error
set deleteHistory to 0
end try

-- Let's delete the application's downloads folder
try
set deleteDownloads to 1
do shell script "rm -r /Users/" & myUserName & "/Library/Safari/Downloads.plist"
on error
set deleteDownloads to 0
end try

-- Let's reopen Safari if it was running when the script was called
-- Note: this does not always work! Eeek!
if safariIsRunning then tell application "Safari" to activate

-- Let's conclude by letting the user know everything is OK
end tell
if safariIsRunning then
tell application "Safari" to display alert "Safari has now been cleaned up." message "You can now resume using the application." as informational buttons "OK" default button 1 giving up after 2
else
tell application "Finder" to display alert "Safari has now been cleaned up." message "Changes have already taken effect and will be retained the next time you launch the application." as informational buttons "OK" default button 1
end if

on error TheError
tell application "Finder" to display alert "Safari could not be cleaned because of a script error." message "Below is the error returned by Apple Script, which may assist you in troubleshooting this issue:" & return & return & TheError buttons "OK" default button 1 as warning
end try


What are your AppleScript-based sanity-saving tips?


11 Comments

Neil_McG
2005-08-24 03:54:42
Applescript
Would you be willing to share the script?
waynesmallman
2005-08-24 04:14:06
Applescript
It's the obvious question, isn't it?
F.J.
2005-08-24 05:28:50
AppleScript
Hi!


Thank you for your reply!


Yes, I am most definitely willing to share the script should anyone find it of interest. The reason I did not from the start is that it plays with "rm" commands and, therefore, could be dangerous should anything go wrong.


I'll edit this entry and add it.


FJ

xyz3
2005-08-24 06:16:11
AppleScript
Hi FJ -


If you plan to use rm -rf I suggest using the quoted form command to quote the path:


Example:
set myUserName to (do shell script "whoami")
do shell script "rm -r " & (quoted form of ("/Users/" & myUserName & "/Library/Cookies/" as string))


The shell script line ends up looking something like this: "rm -r '/Users/adminx/Library/Cookies/'"


Also the file at /System/Library/ScriptingAdditions/StandardAdditions.osax has more than a few useful commands you might want to check out.


EX: set myUserName to short user name of (system info)


This is not to say I am condoning using the unix layer, but merely pointing out another way, and other options.



Have fun-


F.J.
2005-08-24 06:22:03
AppleScript
Hi!


Feedback and comments are always most welcome! Thank you very much for taking the time to suggest these options.


I have played quite a bit with integrating UNIX and AppleScript before all the new fancy commands appeared and I shamefully confess I need to brush up on them so a double thanks for pointing them out.


As you can see, the script is prepared for more advanced error detection as well but I haven't implemented that yet.


FJ

leejoramo
2005-08-24 13:25:58
Fast User Switching to a testing account
I have created a Mac OS X user account just for such testing of websites in development. This gives me an easy to clean account, while leaving alone my normal browser settings in my primary account.
aqsalter
2005-08-24 19:00:52
Safari gripes...
First, there is a great Safari alternative: Shiira
http://hmdt-web.net/shiira/en


Second, Neither Safari nor Shira support auto-saving tabs and pages state (ie quitting and reopening opens the old pages). Which is my major gripe. :(

F.J.
2005-08-25 00:48:14
Safari gripes...
Hi again!


Thank you for passing the link along!


Yes, this is one major feature I would like to see added as well but, as I understand it, requires quite a bit of development. OmniWeb does currently offer it and Camino allows you to bookmarks all open tabs with one click, which definitely is a step in this direction.


FJ

F.J.
2005-08-25 00:51:26
Fast User Switching to a testing account
Hi!


Thank you very much for sharing this tip with us!


This is, indeed, whenever possible, the safest course of action. Unfortunately for me, developing sites also requires staying available on chat and mail to give the people I am working with some real-time updates -- and would therefore require that my second account is an exact mirror of the first.


Thanks again!


FJ

iriajx
2005-08-28 10:58:48
Debug Menu
A large portion of what you seek is available throught Safari's own debug menu.


Enter the following in a terminal window:


defaults write com.apple.Safari IncludeDebugMenu 1


F.J.
2005-08-29 00:26:56
Debug Menu
Hi!


Thanks for reminding us of this command!


Yes, this is very true. Actually, most of the cleaning commands I was referring to in the blog are available through Safari's "public" (so to speak) menus. What I like in the cleaning script is its ability to clean it all in one swell swoop but it is true that using a few menu items is an alternative.


FJ