Banning a Site from Top Sites on Safari

We came across an issue with one of our web apps where users running Safari on OS X were spawning hundreds of unterminated sessions. It was killing our servers. It only took a couple minutes of investigation to figure out that the culprit was the Top Sites grid when a user opens a new tab. Now the question became how to stop it.

Since this is a Software as a Service installation, we couldn't exactly muck about in code to kill unterminated but inactive sessions. Someone proposed (and temporarily implemented) a method that blocks based on user agent string. Not a very user-friendly solution, but it keept everyone else running while we figured out a better fix.

My idea was that I knew if you went into edit mode on the Top Sites page, and clicked the "X" on one of the thumbnails, that site would never come back onto the list again. I figured that if we could just write to the list, it should be easy to permanently keep the web app off Top Sites. I just didn't know where to do that.

A couple hours of defaults read commands, and a few trips to Google turned up the best solution I could come up with. It's not exactly as clean as I'd like, but it seems to work well enough for us, until we can get our vendor to limit the number of concurrent sessions per user.

I created a shell script which we can push out to the user computers. The script does the following:


# This script is to ban a URL from appearing in the Top Sites in Safari
# since Top Sites was spinning up hundreds of unterminated sessions per user.

killall Safari                  # Kills Safari                          

echo "Removing Safari Caches"
rm -rf ~/Library/Caches/
rm -rf ~/Library/Caches/\ Caches

echo "Clearing Top Sites List"
defaults write ~/Library/Safari/TopSites.plist TopSites '{ }'

echo "Banning URLs from Top Sites"
defaults write ~/Library/Safari/TopSites.plist BannedURLStrings -array-add
defaults write ~/Library/Safari/TopSites.plist BannedURLStrings -array-add

I don't like that I can't politely quit Safari from the commandline without installing some additional software. Additionally, I'm not really clear why I have to add the URL both with and without the "www." when Safari doesn't seem to have to do that when it automatically adds to the Banned URL Strings array.

Finally, I'm not too cool that I have to clear the whole Top Sites array before making the edits. While I don't have anything pinned there, I can see a situation where someone uses Top Sites as a quick way to get to their favorite sites, and any change there might be a big deal.