Trying to delete Safari cookies; keystroke command doesn't work


(Tim Burton) #1

Am writing a script to delete a specific Safari cookie/cache. To do this manually requires going to Safari -> Preferences -> Privacy -> Manage Website Data -> entering a value into Search text area.

The following script successfully gets to the Manage Website Data dialog box but entering the value fails.

I suspect some sort of trickery on Apple’s to hide the gui elements from being properly accessed by a script, meaning that the usual methods must be hacked somehow. Am using OS X 10.13.6 (High Sierra) and Safari 11.1.2.

Here’s the code so far:

tell application “System Events”
tell process “Safari”
– go to Preferences…
click menu item 4 of menu 1 of menu bar item 2 of menu bar 1
delay 1
tell window 1
click button “Privacy” of toolbar 1
delay 1
– go to Manage Website Data:
click button 1 of group 1 of group 1

		tell sheet 1
			--should enter value BUT  FAILS:
			keystroke deCookie
		end tell

(Tim Burton) #2

OK, I figured it out.


(CK) #3

…aaaaaaannnnd…? Is this to build suspense before the final reveal ?

I would never consider the use of UI scripting to be one of “the usual methods”. It should generally (pretty much always) be the last method one chooses, and possibly not even then. The only notable exception I can think of only manipulates UI elements in the broadest of ways, which is a general-purpose window-resizing/-moving script that covers non-scriptable applications.

So these aren’t the methods that must be hacked. These methods are the hack.

However, any solution you found would be a useful solution to share for the benefit of others looking to do something similar, as different angles on the same problem is rarely a disadvantage.


I did a quick google and found that Safari and other applications store their cookie data in ~/Library/Cookies. Browsing this directory, I notice there’s a file named com.apple.Safari.cookies, which I’m willing to bet plays a role in housing the Safari cookie data. But I haven’t done anything beyond this briefest of inspections, so a little more research to identify this and other files’ exact nature and consequence of deletion would be prudent. Other Safari-specific data files are located in ~/Library/Safari, of which “localStorage” files/folders are likely pertinent, but, again, I wouldn’t be cavalier in destroying files before finding out their purpose. (Also, renaming-rather-than-removing is a prudent initial testing method). Cache data is stored in databases located at ~/Library/Caches/com.apple.Safari.

Anyway, that’s all the investigation I’ve done or probably will do for now.


(Tim Burton) #4

The answer:

set value of text field 1 of sheet 1 to deCookie