So what you want is called an event listener. (See the section "Working with event listeners" in the InDesign scripting guide.)
Save your .jsx file in the "startup scripts" folder. (On a Mac, it's in /Applications/Adobe InDesign CS6/Scripts/startup scripts/.)
#targetengine "session"
app.addEventListener('afterOpen', function(myEvent) {
// afterOpen fires twice: once when the document opens
// and once when the window loads. Choose one,
// ignore the other.
// See: http://forums.adobe.com/message/5410190
if (myEvent.target.constructor.name !== 'Document') {
return;
}
var myDoc = myEvent.target;
// Continue on with your code from here
}