Is there any way to automate clearing cache, cookies and history in Safari? I can run Swift, Python, JavaScript and AppleScript.
This is the AppleScript I have right now, but it is slow, interruptible and basically activates user interface buttons. What I want is something that works in the background and still gets the job done.
--Clear history (also clears cache and cookies)
tell application "System Events" to keystroke "t" using {command down}
tell application "System Events"
click menu item "Clear History…" of menu 1 of menu bar item "History" of menu bar 1 of process "Safari"
try
click button "Clear History" of sheet 1 of window 1 of process "Safari"
end try
end tell
tell application "System Events" to keystroke "w" using {command down}
CodePudding user response:
I found the files where the browsing data is kept. This AppleScript deletes the files and has the same effect as clearing history. Also clears cache and cookies for all applications.
-- Get path of files to delete and folders to empty
set base to "Macintosh HD:Users:User:Library:"
set docs to {"Safari:History.db", "Safari:History.db-lock", "Safari:History.db-shm", "Safari:History.db-wal", "Safari:CloudTabs.db", "Safari:CloudTabs.db-shm", "Safari:CloudTabs.db-wal", "Safari:PerSitePreferences.db", "Safari:PerSitePreferences.db-shm", "Safari:PerSitePreferences.db-wal", "Safari:Downloads.plist", "Safari:RecentlyClosedTabs.plist", "Safari:SearchDescriptions.plist"}
set docs1 to {"Safari:LocalStorage", "Safari:Favicon Cache", "Safari:Touch Icons Cache", "Caches:com.apple.Safari", "Caches", "Cookies"}
-- Delete history files
tell application "Finder"
repeat with i from 1 to length of docs
set doc to item i of docs
set doc to base & doc
try
delete file doc
end try
end repeat
end tell
-- Empty cache folders
tell application "Finder"
repeat with i from 1 to length of docs1
set doc to item i of docs1
set doc to base & doc
try
delete (every item of folder doc)
end try
end repeat
end tell