How can I set Safari to clear cache and cookies on startup?
Solution 1
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.
Solution 2
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
Solution 3
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
Related videos on Youtube
cwd
Updated on September 18, 2022Comments
-
cwd over 1 year
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?
-
HikeMike over 12 yearsNo info is kept during your browser session. The downloads window loses all functionality, as does the browsing history. You cannot reopen closed tabs either.
-
cwd over 12 yearsAlthough @DanielBeck actually accomplished what I wanted to do, if somebody wanted to do private browsing this would work fine. +1.
-
Leo Gallucci over 9 yearsThanks for the script Daniel however I'm getting a race condition sometimes:
Can't get window "Reset Safari" of <<class pcap>> "System Events"
-
HikeMike over 9 years@LeoGallucci Look into the
delay
AppleScript command.