I would like Photoshop to automatically execute the following task for a given folder:
Let me answer the question you actually asked in bold: There is a tool that automatically generates the Javascript for the actions and events that are taking place in Photoshop. It is called the Script Listener. After using the script listener to record your actions, review the log and make your selective edits.
To begin using the Script Listener
To get your new fangled script into Photoshop place the file you've created with a jsx extension into C:\Program Files\Adobe\Adobe Photoshop CS5\Presets\Scripts.