If you want to omit anchored objects special characters as well, throw in another line:
stringOfTextContents = app.selection[0].contents;
$.writeln(stringOfTextContents.length);
// XML tags, Index markers, Notes markers
stringOfTextContents = stringOfTextContents.split("\uFEFF").join("");
// Anchored object markers
stringOfTextContents = stringOfTextContents.split("\uFFFC").join("");
$.writeln(stringOfTextContents.length);
$.writeln(stringOfTextContents);
Hope, that helps.
Uwe
After testing a bit, I can confirm, that special characters in InDesign formatted text like
1. XML tag markers, Index markers, Notes markers: "\uFEFF"
2. Anchored object markers: "\uFFFC"
will lead to empty text files, if the contents of text is used for writing the text files.
There could be other special characters, that would do the same. Maybe check for table special characters…
Here a snippet that successfully is writing selected text with XML markers and anchored objects to a text file:
var contents = app.selection[0].texts[0].contents;
$.writeln(contents.length);
var stringOfTextContents = contents.split("\uFEFF").join("").split("\uFFFC").join("");
$.writeln(stringOfTextContents.length);
reportPath = app.activeDocument.filePath.fsName;
reportName = app.activeDocument.name.replace(".indd",".csv");
writeReport(reportPath, reportName, stringOfTextContents);
function writeReport(fPath, fName, str)
{
var rept = new File(fPath+"/"+fName );
//alert("rept " +rept)
rept.open("w");
var wBool = rept.write(str);
rept.close();
}
Case closed, I think.
Uwe