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?