Skip to content

Instantly share code, notes, and snippets.

@eip
Last active May 27, 2026 10:27
Show Gist options
  • Select an option

  • Save eip/33eeef6ae5950018479a6209496aedd8 to your computer and use it in GitHub Desktop.

Select an option

Save eip/33eeef6ae5950018479a6209496aedd8 to your computer and use it in GitHub Desktop.
Clear all cookies for the active Safari tab
-- clear-cookies.applescript
-- Clears ALL cookies for the active Safari tab by driving
-- Web Inspector's Storage panel via Accessibility API.
-- Uses polling loops instead of fixed delays — faster and more reliable.
--
-- Prerequisites:
-- Safari > Settings > Advanced > "Show features for web developers" — ON
-- System Settings > Privacy & Security > Accessibility — grant Terminal/iTerm
tell application "Safari" to activate
delay 0.3
tell application "System Events"
tell application process "Safari"
-- Open Web Inspector (skip if already open)
if exists menu item "Show Web Inspector" of menu "Develop" of first menu bar then
click menu item "Show Web Inspector" of menu "Develop" of first menu bar
else if exists menu item "Connect Web Inspector" of menu "Develop" of first menu bar then
click menu item "Connect Web Inspector" of menu "Develop" of first menu bar
else
log "[OK] Web Inspector already open"
end if
-- Wait for Web Inspector group to appear
-- window > splitter group > tab group > group (AXIdentifier = WebInspectorWebView)
repeat 50 times
set found to false
try
if exists (first group of first tab group of first splitter group of first window ¬
whose value of attribute "AXIdentifier" is "WebInspectorWebView") then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "ERROR: Web Inspector group not found."
return
end if
log "[OK] Web Inspector group found."
-- Resolve Web Inspector once
set wi to first group of first tab group of first splitter group of first window ¬
whose value of attribute "AXIdentifier" is "WebInspectorWebView"
-- Step 1: Wait for Storage tab, then click
repeat 50 times
set found to false
try
if exists radio button "Storage" of first tab group of ¬
first UI element of first scroll area of first group of wi then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "ERROR: Storage tab not found."
return
end if
click radio button "Storage" of first tab group of ¬
first UI element of first scroll area of first group of wi
log "[OK] Storage tab clicked."
-- Step 2: Wait for Cookies row, then click
repeat 50 times
set found to false
try
if exists (first row of first outline of group "Storage" of ¬
group "Navigation" of group "Storage" of first UI element of ¬
first scroll area of first group of wi) then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "ERROR: Cookies sidebar row not found."
return
end if
click first row of first outline of group "Storage" of ¬
group "Navigation" of group "Storage" of first UI element of ¬
first scroll area of first group of wi
log "[OK] Cookies sidebar row clicked."
-- Step 3: Wait for Clear Cookies button, then click
repeat 50 times
set found to false
try
if exists (first button of group "Storage" of first UI element of ¬
first scroll area of first group of wi whose description is "Clear Cookies") then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "ERROR: Clear Cookies button not found."
return
end if
click (first button of group "Storage" of first UI element of ¬
first scroll area of first group of wi whose description is "Clear Cookies")
log "[OK] Clear Cookies button clicked."
log "[DONE] Cookies cleared successfully!"
-- Close Web Inspector (poll — menu updates asynchronously)
repeat 50 times
set found to false
try
if exists menu item "Close Web Inspector" of menu "Develop" of first menu bar then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "WARN: Close Web Inspector menu item not found."
return
end if
click menu item "Close Web Inspector" of menu "Develop" of first menu bar
log "[OK] Web Inspector closed."
-- Reload the page (poll — menu updates asynchronously)
repeat 50 times
set found to false
try
if exists menu item "Reload Page" of menu "View" of first menu bar then
set found to true
end if
end try
if found then exit repeat
delay 0.1
end repeat
if not found then
log "WARN: Reload Page menu item not found."
return
end if
click menu item "Reload Page" of menu "View" of first menu bar
log "[OK] Page reloaded."
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment