Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to sanitize Safari Web Browsers
I recently moved from Firefox to Safari after upgrading to Tiger. One thing I missed was the ability to 'sanitize' (clear history, cache, cookies, saved searches etc.) my browser. Although Safari has a Private Browsing, feature I prefer the following method, which lets me keep my cache, history, and cookies until I want them gone, instead of not creating them in the first place.

I'm new to both the UNIX shell and AppleScript, but managed to create this simple AppleScript which executes a shell script. As you can see, it deletes the current user's Safari cache folder, cookies file, history and download history file. It also deletes the recent Google searches from Safari's preferences file.
try
do shell script "rm -r ~/Library/Caches/Safari
  ~/Library/Cookies/Cookies.plist ~/Library/Safari/Downloads.plist 
  ~/Library/Safari/History.plist ; 
  defaults delete com.apple.safari RecentSearchStrings"
end try 
Note: The do shell script line has been broken into four lines here for narrower display. Make it one line only, with a space replacing each line break.

One thing I couldn't manage to implement was something that would delete any saved form searches (like the one at MacUpdate). Safari seems to save these to com.apple.Safari.plist as keys beginning with com.apple.WebKit.searchField, followed by different names for each form.

I've only used this with Safari 2, but assume everything is stored in the same place with Safari 1, so it should work with that too.

[robg adds: You could also use the Reset Safari menu item, but you'd also lose your saved usernames and passwords if you do so...]
    •    
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[44,704 views]  

An AppleScript to sanitize Safari | 18 comments | Create New Account
Click here to return to the 'An AppleScript to sanitize Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to sanitize Safari
Authored by: abyone on Nov 11, '05 08:08:08AM

<blockquote>
I prefer the following method, which lets me keep my cache, history, and cookies until I want them gone, instead of not creating them in the first place
</blockquote>

I'm pretty sure that safari does create them (history, cache, and cookies) when using private browsing, it just gets rid of the changes after you turn it off (i.e. until you wan them gone!).

mmmmm, run simple test.... yup, history still shows up in private browsing mode. Should be simple enough to test the cookies are kept too - but I leave that test as an exercise to the reader.

On another note, I find it hilarious that everybody knows the exact reason why this feature exists, and almost nobody mentions that when discussing the feature.



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: raider on Nov 11, '05 08:50:25AM

What would that reason be?

I can think of a few...

Like you are at work, and you want to look for a better job - but you don't want any traces of that. Or you want to post to a website that is critical of your company. Or you work for a large Tobacco or Oil or Energy company and want to be a whistle blower. All things that need your tracks erased.

Or you want to browse information about your governments illegal activities. Or find out how to become a dissident. Even how to find council when your American first ammendment rights are violated.

Perhaps you want to browse Sony, or MPAA, or RIAA websites. You don't want them tracking you with persistent cookes. How about adbots and spambots and cookies that track your behavior across sites. We don't want any insideous behavior such as that. Need to keep our computer clean.

What if you want to order your spouse or kids a Christmas present, anniversary gift, or birthday extravaganza - and you know they will snoop. You need to erase the evidence!

.

.

.

Or Pr0n.


By the way, I also recommend tunneling your traffic through a cloaking SSH tunnel or HTTPS proxy, if you don't want what you browse to be sniffed or logged.

Not that I would need to, just saying.... You know. Informational purposes. This *is* a hint site, right?

What?

:)



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: ekc on Nov 11, '05 08:54:44AM
On another note, I find it hilarious that everybody knows the exact reason why this feature exists, and almost nobody mentions that when discussing the feature.
So with...er...that in mind, should you be paranoid, you might consider replacing rm with a minimum srm -s or better to thwart at least a software-based file recovery.

[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: brodan on Mar 28, '06 11:22:28PM

Oh, nice. i used this and it wiped out everything in Safari, and Firefox and all system preferences!!!! I am having to rebuild everything back like I had it. I also now get three error messages when something goes wrong in any app, including the system. What is up with that and how can I fix it?

---
Loving the Mac. User since 1989.



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: Anonymous on Nov 11, '05 08:48:55AM

You can also use Safari CleanUp:
http://scriptbuilders.net/files/safaricleanup0.5.4.html



[ Reply to This | # ]
Resetting Safari
Authored by: geoship on Nov 11, '05 08:52:42AM

From Safari help:

Resetting Safari

"If you use a shared or public computer, you may want to reset Safari to prevent other people from seeing what you have been doing.

Resetting Safari clears the history, empties the cache, clears the Downloads window, and removes all cookies. It also removes any saved user names and passwords or other AutoFill data and clears Google search entries.

In addition, any open windows are closed and a new window opens. The new window has a new history for the Back and Forward buttons and the SnapBack buttons.

Choose Reset Safari from the Safari menu and click Reset.

If you reset Safari, then cookies saved by other applications, such as Sherlock, may also be removed."



[ Reply to This | # ]
Private Browsing in Safari
Authored by: magnamous on Nov 11, '05 01:22:13PM
Also from Safari Help:

Private Browsing:

Protecting private information on shared computers

When you browse the Internet, Safari stores information about the websites you visit on your computer. That information can include the contents of the webpages you've viewed and any information you've entered at a site, such as usernames, passwords, and credit card numbers. Other people who use your computer may be able to view that information.

If you don't want Safari to store this information, you can turn on private browsing. When private browsing is turned on

  • Webpages are not added to the history. (You can still use the Back and Forward buttons to return to webpages you've viewed in a window.)
  • Items are automatically removed from the Downloads window when private browsing is turned off.
  • Information isn't saved for AutoFill, including names and passwords.
  • Searches are not added to the pop-up menu in the Google search field.
  • Cookies are deleted when private browsing is turned off.


[ Reply to This | # ]
Private Browsing in Safari
Authored by: TheCat on Jul 09, '06 10:53:46AM

I am trying to do the opposite. I am trying to print Safari's History, and hopefully more than what currently shows. I have discovered my husband is into porn. Now I need to print out a listing of these sites to confront him.

Thank you for any assistance.



[ Reply to This | # ]
Private Browsing in Safari
Authored by: bogdescu on Dec 01, '07 05:51:12PM

15 months later - I guess it's okay to respond to a troll? I just did. :)



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: b00le on Nov 11, '05 08:52:58AM
There is the command Reset Safari, though it might do more than you want:
Resetting Safari erases your browsing history, empties the cache, clears the Downloads window, and removes cookies. It also removes any saved names and passwords or other AutoFill text and clears Google search entries.


[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: cwatts on Nov 11, '05 09:35:52AM

How about "Empty Cache-cmd-option E" which appears to empty ~/Library/Caches/Safari/??/*
and the history...

?

---
sttaw sirhc



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: ocdinsomniac on Nov 11, '05 09:43:37AM

I don't understand why this needs to be an AppleScript. Couldn't you just make it a simple shell script, with a .command suffix, and it would behave the same way? Is there some reason people avaoid opening a terminal window like the plague? I don't get it...

Sorry, BTW, if this sounds overly snarky. Pre-coffee mood disorder.

But seriously:Why?



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: oink on Nov 14, '05 10:39:42AM

Yes, I find Applescript much more confusing that shellscript. They are equally harmful if you make a typo, might as well get closer to the source.

---
blurred visionary



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: Provider on Nov 12, '05 02:39:11AM

But....

1. How do you change the focus on the 'Reset Safari' window from 'cancel' to 'reset' to allow easy keyboard access.

2. Is there a keyboard shortcut that could allow resetting without having to go through the superfluous process of: Safari/Reset Safari/Reset



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: cvarrick on Jan 16, '06 05:48:42PM

First off, set up an application keyboard shortcut in Keyboard & Mouse preferences (I use "command + option + shift + e"). Then make sure "Full keyboard access" is set to "All controls" (can be toggled by pressing "control + F7").
So, in safari press your custom shortcut, press tab, then press the spacebar.



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: jeff_lamarche on Nov 14, '05 08:28:03AM
Personally, I would have implemented this as a shell script since there's no Applescript functionality being used, but that's a personal preference thing for sure.

If you're really paranoid, install srm and use it instead of rm in your script or instead of that, you could use Applescript to drag the same files to the trash and do a secure empty.

[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: thecloud on Jan 16, '06 09:30:04PM

Just a FYI: srm is already installed in Tiger, and is part of the Darwin source base. Apple's version of srm (1.2.8/Apple, as of 10.4.4) will overwrite a file's resource fork as well as its data fork. You really don't want to replace it with the plain 1.2.8 version from sourceforge, which does not have that capability.



[ Reply to This | # ]
An AppleScript to sanitize Safari
Authored by: Mason Rove on Jun 22, '10 06:01:29AM
You can reset Safari using the following AppleScript (GUI scripting must be enabled):
tell application "System Events"
	tell process "Safari"
		set frontmost to true
		click menu item "Reset Safari…" of menu 1 of menu bar item "Safari" of menu bar 1
		--delay 1 --may be uncommented if needed
		click button "Reset" of window 1
	end tell
end tell


[ Reply to This | # ]