1 Reply Latest reply on Oct 24, 2015 6:28 PM by miguels84664263

    Script to export to csv

    miguels84664263 Level 1

      We are trying to write a simple script to export all stories in an Indesign file

      we have only two styles Headline, which is the headline of each article

      and text, that is the body of the article

      we wat to export to a csv file (text) so we can imported in a series of applications that we have like this:

      headline, Text (series of paragraphs)

      "xxxxx","xxxxxxxxx x x x x xxx xxxxx xxxx xxx"

      "xxxxx","xxxxxxxxx x x x x xxx xxxxx xxxx xxx"

      "xxxxx","xxxxxxxxx x x x x xxx xxxxx xxxx xxx"

      but we came out with a problem, when we try to write to a file the string variable of the "contents of the paragraphs" won't write to the file, we can display them in an alert dialog, but don't write them in a text file

      here si the code:

      //Exportascsv.js
      
      
      var myStory, myParagraph, myParagraphTemp, myString, myTag, myStartTag;
      var myEndTag, myTextStyleRange, myTable;
      
      
      if(app.documents.length !=0){
        if(app.documents.item(0).stories.length != 0){
      //Open a new text file.
          var myTextFile = File.saveDialog("Save CSV As", undefined);
      //If the user clicked the Cancel button, the result is null.
          if(myTextFile != null){
      //Open the file with write access.
            myTextFile.open("w");
            var myCounterArt = 0;
      //walk through the stories.
            for(var myCounter = 0; myCounter < app.documents.item(0).stories.length; myCounter++){
              myStory = app.documents.item(0).stories.item(myCounter);
      // we have stories inside stories (articles in articles sometimes)
              var myParagraphCounter = 0;
              myParagraphTemp = "";
      
      
              myString = "";
              do{
                myParagraph = myStory.paragraphs.item(myParagraphCounter);
                myParagraphTemp = "";
                switch(myParagraph.appliedParagraphStyle.name){
      //Headline paragraph style
                    case "Headline":
                      myString = '"'+myParagraph.contents+'","';
                      myParagraphCounter++;
                      break;
                    case "Texto":
      //Text paragraph style
                      myString = myString+myParagraph.contents;
                      myParagraphCounter++;
                      break;
                    default:
                      myParagraphCounter++;
                      break;
                }
              } while (myParagraphCounter < myStory.paragraphs.length)
              myString = myString+'"';
              myTextFile.writeln(myString);
            }
            myTextFile.write("\r");
            myTextFile.close();
          }
        }
      }
      
      

      the problem is the line 46, we don't know if we have to strip some characters or transform the contents to a only text variable

      Thank0s in Advance