I've spent the last day tracking down the reason you sometimes get these errors in photoshop CS5 and 5.5 (and probably in 6, but I don't have that yet).
If you search for this problem most people recommend trashing the photoshop preferences, and most of the time that works. However, it didn't for me, and anyway trashing prefs always feels a bit cargo-cult, so I dug a bit deeper.
Using the ExtendScript Toolkit, it was clear that when photoshop loaded, ScriptUI was not getting created - any of the objects associated with ScriptUI returned undefined in the console, but the rest of ExtendScript was up and running. Even more odd was that these objects DID exist right up until you started Photoshop.
Turns out that our CS5 install has TWO copies of the ScriptingSupport.8li plug in - one in /Plug-ins/Extensions the other in /Plug-ins/Automate. Photoshop seems fine with multiple copies of a plugin, and only loads one of them. However, only the plugin in /Extensions has the additional ScriptUIFlexServer.swf, ScriptUIFlexServer-app.xml and ScriptUIFlexPhotoshop.swf files alongside it that appear to be necessary for ScriptUI to load properly. This appears to be a historical side-effect of the way we install CS, but it's a pretty easy situation to get into.
So, when Photoshop starts it chooses which of the plugins to load and if it loads the one in /Automate, ScriptUI is broken and anything that tries to open a window fails with Error 22.
However, photoshop also caches the plugin locations, so once it's loaded the wrong one, it always does so, until you trash the preferences, whereupon it chooses which one to load again.
This is why deleting the prefs works most of the time, even for our installation.
The real solution, though, is to make sure you have only one plugin that has the ScriptUI files with it
For us, this meant deleting ScriptingSupport.8li from /Plug-ins/Automate, but you might find you need to reinstall the ScriptUI files
Here's hoping that this helps everyone else with this problem that couldn't solve it by deleting preferences.
Mike Trinder
Miller Hare Ltd
www.millerhare.com