Are you using a script engine?
Your script should start with:
CS4 did have afterOpen (as did CS3).
For the full list of events available in CS4 (and CS3) check the InDesign CS4 (or CS3) Scripting Guide (pdf).
Prior to CS5, which adds support for afterOpen being associated with Window objects, working with the afterOpen event was very frustrating because it is delivered too early. The error says the object no longer exists. That's because the composer of that error message couldn't imagine that you'd be able to try to do something to an object before it exists.
The only solution I know of (other than upgrading to CS5) is to use the Rorohiko plug-ins http://www.rorohiko.com/ to give yourself more events to work with.
Thanks for all your help. Maybe I am going about this the wrong way and you might have some advice about a better route. Basically
I want to automate opening a document, importing some XML and exporting a PDF. I have a script developed which does all that I'm just hunting for a way to automate running it. Seemed like the easiest way would be to specify the .indd file on the command line then hook into the open event and then run my script. Other ideas?
I'll look at Rorohiko in the meantime.
If you don't reference the window, I think you should be okay...
This wouldn't be the first time that debugging code has caused my problems. I tried a little experment and reduced my script even further to
var eventListener1 = app.addEventListener("afterOpen", openEvent, false);
and that still threw the same error. I don't see how it could be any simpler than this. So was afterOpen in CS4 completly and utterly broken? It can't be, there might be something I was doing horribly wrong. I went back to the scripting guide and found that I was missing
Which I believe was suggested earlier by Harbs. Adding that in solved the problem. Thanks for your help!