Skip to main content
Inspiring
March 24, 2016
Answered

Report Writing...

  • March 24, 2016
  • 2 replies
  • 1260 views

Hi,

     When writing the contents as report, report is generated as empty.  Because the contents have some character (XML Tags ie., ::).  So Could not able to write the contents.

     I have used find and replace but its not removed.  How can I get the contents to write report.

    

This topic has been closed for replies.
Correct answer Laubender

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

2 replies

Community Expert
March 25, 2016

Hi Sudha,

the contents should be writable. Even when XML tags are present.
However, they would perhaps reflect as additional white space in the text file.

If you want get rid of them, search for <FEFF> characters with Text Search/Replace in InDesign.

Or use RepExp with ExtendScript and replace the special characters before writing the report to a text file.

Here an example.

Select some text with XML tags in InDesign and run the snippet:

var myRegEx = "\\x{FEFF},g,\"\"";

var myString = app.selection[0].texts[0].contents;

alert(myString.replace(myRegEx));

Uwe

Sudha_KAuthor
Inspiring
March 26, 2016

Hi,

    Thank you....  Its still generate an empty csv for me. Below is the used code.

var myRegEx = "\\x{FEFF},g,\"\""; 

var myString = app.selection[0].texts[0].contents; 

var myString1 = myString.replace(myRegEx);

alert(myString.length + " = " + myString1.length)

reportPath = app.activeDocument.filePath.fsName;

reportName = app.activeDocument.name.replace(".indd",".csv");

writeReport(reportPath, reportName, myString);   

function writeReport(fPath, fName, str)

{

    var rept = new File(fPath+"/"+fName );       

    alert("rept " +rept)

    rept.open("w");

    var wBool = rept.write(str);       

    rept.close();

}

Contents length is same before and after process of regex.

- Sudha K

Community Expert
March 26, 2016

Hi Sudha,

sorry, I suggested the wrong method.

We have to inspect every single character in the selection.

var stringArray = app.selection[0].characters.everyItem().contents;

alert(stringArray.length);

for(var n=0;n<stringArray.length;n++)

{

    if(stringArray.charCodeAt() == 65279){stringArray = ""};

};

var string = stringArray.join("");

alert(string.length);

Uwe

pixxxelschubser
Community Expert
Community Expert
March 24, 2016

Is this a scripting question?

Please show the relevant part of your code and at the best an example IDML file.