Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Delete Single Safari Cookie with AppleScript

UI Element Scripting can be accomplished using AppleScript.


I needed a script that would delete my Netflix cookie from Safari and sign me back in.


I used AppleScript to create the following "Reset Cookie" application.


1. Open AppleScript Editor in ~\Applications\Utilities

2. Paste in the following code, then click "Compile" then "Run"

3. Save as an Application (Run Only)


set theWebsite to "netflix"

tell application "Safari" to activate

delay 2

tell application "System Events" to tell process "Safari"


keystroke "," usingcommand down

tell window 1


clickbutton -3 of tool bar 1


delay 1


clickbutton "Details…" of group 2 of UI element 2 of row 1 of table 1 of UI element 1 of scroll area 1 of group 1 of group 1


delay 1


keystroketheWebsite


keystroketab


delay 1


keystroke "a" usingcommand down


delay 1

click button "Remove" of sheet 1

click button "Done" of sheet 1

end tell


keystroke "w" usingcommand down

tell application "Safari" to set the URL of the front document to the "https://signup.netflix.com/Login"


delay 2


keystrokereturn

end tell


Notes:

- there is a variable called theWebsite which specifies the name of Cookie I want to look for in Safari's Privacy settings

- the script opens Safari, opens Preferences, and selects the Privacy Tab

- the trick is to target the "Details..." button, which is buried deep in the UI hierarchy

- once that's open, theWebsite value is entered into the search field, the results are selected and removed.

- finally, close the dialog, and tell Safari to open the new webpage (for me, the Netflix sign in page)

- since keychain has my login info, I just tell the script to hit "Return" (the delay gives the page a chance to load, which is important - set that higher for slower connections


Make sure to read this first to make sure your Mac is configured to accept UI Scripting via the Accessability System Preferences and to learn more about UI Scripting.


http://www.macosxautomation.com/applescript/uiscripting/index.html

Posted on Jun 7, 2012 11:29 PM

Reply
7 replies

Jun 8, 2012 10:17 AM in response to twtwtw

You've clearly missed the point. This is to create a 1-click automated process. What you describe requires locating a file in an invisible directory, opening the plist file in a text editor, and locating the correct text in the file and modifying it correclly. None of which you should do while Safari is open, and which is much more complicated from a script perspective.

Jun 8, 2012 1:03 PM in response to SilentMountain

Update


I was having unreliable results with this line:


clickbutton -3 of tool bar 1


so I changed it to:


clickbutton "Privacy" of tool bar 1


Other Notes:


  • Increased the first delay to 4, since if Safari was closed it didn't have time to spawn a new window before kicking off the Preference Window process. This would throw an error for the last line to navigate to said URL. If you don't use the last URL line, the delay should be ok. Depends on how long it takes your machine to load Safari.
  • The (Run Only) suggestion makes it so you can't edit the script later, so ignore that one :/
  • Many of the delays in the middle of the code are just for user benefit - it seems to run fine without them, but I like being able to see it happening.

Oct 29, 2012 10:01 PM in response to SilentMountain

Thanks for this script, works nicely for me except for the Details... button line. In my version of Safari (6.0.1), I had to change it to:

click button "Details…" of group 1 of group 1


For the future reference of other people who get an error on that line (the error is simply that it is unable to retrieve the element as described), I identified the correct reference to the button by opening Safari, opening the preferences page, going to the Privacy tab, then creating and running this one-line script:

tell application "System Events" to get entire contents of window 1 of process "Safari"


The Result (and the Replies) should list all of the elements of the Window (it is possible to narrow this down by modifying the script, but this approach will give you everything and you can just scroll until you see "Details..."). Find "Details...", it will look something like this:

button "Details…" of group 1 of group 1 of window "Privacy" of application process "Safari" of application "System Events"


Because the OP's script already has us in in a "tellwindow 1" block, we can (and must) omit the latter part of the line starting with "of window".

Apr 2, 2013 9:59 AM in response to fasgf354qvt2b5

I had to tweak this with some delays for Safari 6.0.3, and eliminated the auto-load at the end (since I'm not using it for Netflix 🙂 ). This version seems to work consistently:


set theWebsite to "wiley"

tell application "Safari" to activate

delay 2

tell application "System Events" to tell process "Safari"


keystroke "," usingcommand down


delay 2

tell window 1

click button "Privacy" of tool bar 1


delay 1

click button "Details…" of group 1 of group 1


delay 1


keystroketheWebsite


keystroketab


delay 1


keystroke "a" usingcommand down


delay 1

click button "Remove" of sheet 1

click button "Done" of sheet 1

end tell


keystroke "w" usingcommand down

end tell

Delete Single Safari Cookie with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.