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