cookie monster

Yeah, I wouldn’t recommend doing this. It’s old. Probably doesn’t work anymore anyway.

I don’t like cookies so much. The persistent browser types, with expiry dates of 2031 that cause a trail of my identity to be left across Google and other sites. And I don’t like how poorly Safari manages them. Even a checkbox option would be better, to keep the ones I need or don’t mind and to delete all the others instead of manually having to go through them all.

SafariPlus used to do this perfectly, from within the browser, unobtrusively. But since 10.5 and Safari 3, it hasn’t worked. So I changed to Cocoa Cookie, a separate small utility. I had to go to my Applications folder to find it, but was still quick and… then it stopped working, it would open without the window showing, caused much weirdness with Safari’s cookies since the latest version and…

uuuhhh… annoyance.

I found a Perl script a couple of days ago. I suppose it could also be done in AppleScript, and should really learn how to write in that, but it’s rather perfect. Well it runs from Terminal also, and requires some editing, but…
#!/usr/bin/perl

use strict;
use warnings;
use File::Slurp;

### Edit this to your liking (put a pipe character between two words)
my $keepCookiesWith = "gaydargirls|culturedcode|supernaut|dreamhost";

### Put your OS X short username here (there should be a directory with the same name under /Users)
my $userName = "francesdath";

### ### ### Don't edit beneath this line unless you know some Perl
my $path = "/Users/francesdath/Library/Cookies/Cookies.plist";
my @date = localtime();
my $date = sprintf("%04d%02d%02d", $date[5] + 1900, $date[4] + 1, $date[3]);
my $cookies = read_file($path);
rename ($path, $path . "." . $date);

open(WH, ">$path");
print WH <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/
PropertyList-1.0.dtd">
<plist version="1.0">
<array>
EOF
while ($cookies =~ m#(\s*<dict>.+?</dict>)#gs)
{
my $cookie = $1;
if ($cookie =~ /$keepCookiesWith/)
{
print WH $cookie;
}
}
print WH <<EOF;

</array>
</plist>
EOF
close (WH);

So, copy it into your favourite text editor, save as something memorable like ‘SafariCookiecleaner.pl’ and put it somewhere out of the way but not forgettable. You can add new cookies to be saved based on any attribute that appears in the Safari cookie window. I tend to use the domain names, like ‘macosxhints’, though I have one Google cookie I like to keep, with the name ‘PREF’, so I added that also (unfortunately YouTube has one with the same name…). Set the ‘username’ to your Home Folder (probably what appears in the SideBar in the Finder), and then set ‘my $path = “”;’ to ‘/Users/yourhomefolder/Library/Cookies/Cookies.plist’ which is the path to Safari’s cookies file.

Then open Terminal, and change to the directory where you’ve put it, change the permissions to 755 and then run it (quit Safari first).

Well like this:
cd /drag/the/folder/containing/the/script/into/Terminal
chmod 755 SafariCookieCleaner.pl
./SafariCookieCleaner.pl

Open Safari, look in cookies in Preferences and the ones you like should still be there. It makes a backup of the cookies file, so at worst nothing is irreparable. And it makes you look all UNIX by opening Terminal.