Skip to content

Instantly share code, notes, and snippets.

@NSBum
Created March 29, 2017 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NSBum/d5bbad84dd6e9c9c91413a0f370c2403 to your computer and use it in GitHub Desktop.
Save NSBum/d5bbad84dd6e9c9c91413a0f370c2403 to your computer and use it in GitHub Desktop.
Remove cookies from Safari
--
-- Created by: Alan Duncan
-- Created on: 2017-03-29
--
-- Copyright (c) 2017 OjisanSeiuchi
-- All Rights Reserved
--
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- location of domains to expunge
set siteListPath to -- <--- path to your site list, a text file with sites e.g. facebook.com, 1 per line
-- populate list of sites to clean
global deCookie
set deCookie to {}
my populateSites()
-- close the target tabs if any
my closeTabs(deCookie)
-- bring Safari to the front for scripting
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
-- bring up preferences window
keystroke "," using command down
delay 1
tell window 1
click button "Privacy" of toolbar 1
delay 4
repeat with d in deCookie
try
click button "Details…" of group 1 of group 1
on error errStr number errorNumber
# echo to file
display dialog (errStr as string)
end try
delay 1
try
keystroke d
delay 1
select row 1 of table 1 of scroll area 1 of sheet 1
click button "Remove" of sheet 1
end try
click button "Done" of sheet 1
end repeat
end tell -- window 1 (preferences window
-- close the preferences window
keystroke "w" using command down
end tell -- process Safari
end tell -- System Events
-- close the specified tabs
on closeTabs(tabList)
tell application "Safari"
repeat with closeURL in tabList
try
close (every tab of window 1 whose URL contains closeURL)
on error errStr number errorNumber
if errorNumber is -1728 then
display dialog "The prefs window is open"
end if
end try
end repeat
end tell
end closeTabs
-- read the site list
on populateSites()
set sites to paragraphs of (read POSIX file siteListPath)
repeat with nextLine in sites
if length of nextLine is greater than 0 then
copy nextLine to the end of deCookie
end if
end repeat
end populateSites
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment