delete cookies with applescript

Created at: 2021-05-25 11:15:06
Questioner General

Does anyone have an applescript that deletes a set of cookies from Safari in Big Sur?



Answer #1:

By: Otha O'Kon

Does anyone have an applescript that deletes a set of cookies from Safari in Big Sur?



Answer #2:

By: Wilber Barton

It is simpler to select Safari : Clear History : All History, then writting GUI scripted AppleScript that will eventually break.

Answer #3:

By: Hailee Johns

I don't want to lose my history. Is there no GUI-independent way to go about this? How about a script to surgically edit cookies.plist?

Answer #4:

By: Chloe Yundt

Here is a GUI AppleScript that I wrote three-years ago that clears all Safari cookies, but leaves history alone. To my surprise, it just worked with Safari 14.0.2 on macOS 11.1. Once you start it, resist the temptation to click any buttons that may appear on screen, as the script will take care of those, including the final alert that times out and disappears.

Copy and paste into the Script Editor, click the hammer (compile) icon, and then run it.

-- cookie_remove.applescript
-- Uses GUI scripting which can break with any future release of Safari
-- Due to recent changes, this script may not work with older Safari versions
-- Tested with Safari 11, 12 (High Sierra/Mojave) and 13 (Mojave/Catalina), Safari 14.0.2 (macOS 11.1)
-- reference:
-- Version 1.2
-- VikingOSX, 2017-10-26, Apple Support Communities

tell application "System Events"
	tell application process "Safari"
		set frontmost to true
		keystroke "," using command down
		delay 1
		tell window 1
			click button "Privacy" of toolbar 1 of it
				click button "Manage Website Data…" of group 1 of group 1
				-- give cookies time to load in their scrollable window
				delay 3
				click button "Remove all" of sheet 1
				-- yes, we are sure. remove all cookies with a return
				key code 36
				click button "Done" of sheet 1
			on error errmsg number errnbr
				keystroke "w" using command down
				display alert "No cookies to remove" giving up after 5
			end try
		end tell
		keystroke "w" using command down
		display alert "All cookies removed" giving up after 5
	end tell
end tell

Answer #5:

By: Mr. Josue Sporer I

Thanks for this! Have you considered how you'd update it to remove a set of targeted cookies?

Can you point me to where I can learn to determine how to reference fields in a GUI?



Answer #6:

By: Merlin Parisian

When I wrote this, the original request that I was responding to wanted to whack all cookies. I have never tried to target one specific cookie much less multiple discrete cookies, in that Manage Website Data… panel, and it might turn into a time sink pursuing that capability.

In the Script Editor File menu : Open Dictionary… and scroll down to System Events. Select that, and once it opens, select the Processes Suite. This is the basis of GUI scripting along with knowing what underlying Cocoa design artifacts (sheet, row, panel , list, button, etc.) that Apple has chosen to build the interface. Apple has and continues to change the visual design features, and that is what frequently makes GUI scripts fragile.