Actually, if you are using Photoshop CS, there's an easier way to do it. Of course, it is not as efficient as if you were to write the whole thing from scratch but a good starting point. All you have to do is customize enough of it to do what you want it to do...
There's a built-in feature called the ScriptListener. This file is a .8li file and is resident on your c:\...\adobe\photoshop cs\scripting guide\utilities\. Copy this file to c:\...\adobe\photoshop cs\plug-ins\adobe photoshop only\automate.
After you do that, start Photoshop, bring up the Actions window. RECORD (not play) the actions you require, you can bring up other commands such as export etc., using the left arrow key on the Actions window. After you are done, there will be two new files called c:\scriptinglistenerjs.log and c:\scriptinglistenervb.log, which records all the actions you just performed. When you are done, remember to move the file back or the log will continue recording actions you play and record. If you batch process 200 files, you can imagine how big the log file will be...
Start a new text file, copy the relevant script over, replace the variables (Photoshop defaults are boring - ids and descs), add in the if...then, do...loop commands and you have your new script file.
In c:\...\adobe\photoshop cs\scripting guide\, there are Visual Basic and Javascript Reference Guides for Photoshop features...
Of course, I am no javascript expert (in fact, I am prefer Visual Basic to Javascript, but it is easy to screw up if one is to try to get Visual Basic to talk directly to Photoshop) and am learning as I go, so any additional tips after someone else experiments with the feature is most welcomed. I have also not figured out how to use automate the script for every picture file in a specific folder and to save as a different name convention a la the Batch feature on Photoshop...