Hope you didn't get too mad at me. Like i said, your first version was good for 99.99% of the times. and if someone names his files like that, he deserves all the crashes he will get. Still it's a fun exercise to find/fix such problems and you have camed a long way from your first tries at scripting.
So..
there are more ways of doing the filen name stuff.
one of them is what you did, to use a harcoded name.
another is using split's method second parameter, that limits the number of found parts
var myDocName = myDoc.name.split(".indd",1).join("_contents.txt");
Another is to use a regular expression:
var myDocName = myDoc.name.replace(/\.indd$/,".txt");
(i learned on this forum that the replace method takes as argument a RegEx. - thank you, Peter)
later edit: if the doc name has no extension whatsoever, the last method won't work as expected.
make it:
var myDocName = myDoc.name.replace(/\.indd$/,"")+".txt";