Note that this script relies heavily on GUI scripting to compensate for the pathetic built-in scripting of Safari. A window will pop-up at the start of the script if GUI scripting is not turned on. It can be enabled in the Universal Access System Preferences panel (check the Enable access for assistive devices box).
[robg adds: I've also mirrored the script here on macosxhints.com. I tested it, and it works as described.]

