Is there a way I can set Safari to clear cache and cookies on startup? Maybe a flag I could use when launching the application, or perhaps even a bash or applescript that would delete files on the hard drive and then launch the app?
4 Answers
Use the following AppleScript code and save it e.g. as an application in AppleScript Editor, or as a Service consisting of a single Run AppleScript action in Automator.
tell application "Safari" to activate
tell application "System Events"
tell application process "Safari"
tell menu bar 1 to tell menu bar item "Safari" to tell menu 1 to tell menu item "Reset Safari…" to click
tell window "Reset Safari" to tell button "Reset" to click
end tell
end tell
This will activate Safari, launching it if it's not running, and then open and submit the "Reset Safari…" menu item.
-
Thanks for the script Daniel however I'm getting a race condition sometimes:
Can't get window "Reset Safari" of <<class pcap>> "System Events"
Nov 8, 2014 at 1:24 -
1
Hope this helps ... new to the Applescript thing myself but I put these together from looking over other examples like the one above this.
These scripts are saved as applications. Perhaps instead of saving as an application,creating these as a Service off of the finder menu... maybe with a keyboard short cut would be a good thing too.
The first script (Safari ON) will open Safari in private browsing mode... then reset safari ...then clear the cache.
The second script (Safari OFF) will reset safari... clear the cache.. then close safari.
They both seemed to work fine for me but like I said... I'm new to this, I've only been at it for two days.
If you find the scripts incorrectly done... or can improve them in some way feel free to post any recommended changes for everyone.
=================
# Safari ON
# tell application "Safari"
activate
# end tell
# tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "Safari"
tell menu "Safari"
click menu item "Private Browsing"
end tell
end tell
end tell
end tell
tell application process "Safari"
tell menu bar 1 to tell menu bar item "Safari" to tell menu 1 to tell menu item "Reset Safari…" to click
tell window "Reset Safari" to tell button "Reset" to click
end tell
# end tell
# tell application "System Events"
# tell application process "Safari"
tell menu bar 1 to tell menu bar item "Safari" to tell menu 1 to tell menu item "Empty Cache…" to click
end tell
# end tell
# tell application "System Events"
tell application process "Safari"
click button "Empty" of front window
end tell
# end tell
To do the same thing but on close:
# Safari OFF
# tell application "System Events"
tell application process "Safari"
tell menu bar 1 to tell menu bar item "Safari" to tell menu 1 to tell menu item "Reset Safari…" to click
tell window "Reset Safari" to tell button "Reset" to click
end tell
# end tell
# tell application "System Events"
tell application process "Safari"
tell menu bar 1 to tell menu bar item "Safari" to tell menu 1 to tell menu item "Empty Cache…" to click
end tell
# end tell
# tell application "System Events"
tell application process "Safari"
click button "Empty" of front window
end tell
# end tell
# tell application "Safari"
quit
# end tell
Isn't it easier to use safari in private browsing mode, in that way, no info is kept in the first place
To make safari start with private browsing enabled follow the following step
First, launch the Universal Access system preference and enable the Enable Access for Assistive Devices option.
launch Script Editor (within the AppleScript folder inside the Applications folder) and enter the following script:
tell application "Safari" activate end tell tell application "System Events" tell process "Safari" tell menu bar 1 tell menu bar item "Safari" tell menu "Safari" click menu item "Private Browsing" end tell end tell end tell end tell end tell
Save the script as an application and then use that application to launch Safari. When you do, Safari will launch and Private Browsing will be enabled.
Reference : http://www.macworld.com/article/139714/2009/03/enableprivatebrowsing.html
-
No info is kept during your browser session. The downloads window loses all functionality, as does the browsing history. You cannot reopen closed tabs either.– Daniel Beck ♦Jan 11, 2012 at 20:36
-
Although @DanielBeck actually accomplished what I wanted to do, if somebody wanted to do private browsing this would work fine. +1.– cwdJan 12, 2012 at 3:05
Clear History
Here is my version of a clear history script for Safari. As it is I have it setup for German, but it can easily be modified.
Features:
Completely automates the deletion of history under the history menu and top sites (this can be toggled by removing the not
in the if statement below).
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
tell menu bar item 6 of menu bar 1
get name of menu items of menu 1
click menu item "Verlauf löschen …" of menu 1
end tell
-- Window 1 immediately becomes the ARE YOU SURE pop up if history is not empty
--return UI elements of window 1
if role description of window 1 is "Dialog" then -- Ensures that the dialog pops up
-- THE FOLLOWING ENSURES TOP SITES IS CHECKED
set theCheckbox to checkbox 1 of window 1
tell theCheckbox
if not (its value as boolean) then click theCheckbox
end tell
click button "Löschen" of window 1
end if
display dialog "Safari history been cleaned." with title "Safari Cleaner Script" with icon caution
end tell
end tell
--quit application "Safari"