Hello.
This script can be easily installed by adding it to your /Adobe app/Presets/Scripts directory. Once it's installed it can be run from the File -> Scripts menu in Photoshop. Feel free to edit it to accomodate your needs. The code is well commented so you can understand what is going on.
Enjoy! Patrick