12 Replies Latest reply on Oct 7, 2015 2:44 PM by BSKTCreation

    Export Text by textFrame to a Single Text or CSV File

    cathykka

      Indesign Doc with

      Text boxes on every page that will hold

      Title, Description, Category

      I am trying to create a script that will loop through each page

      Locate

      .textFrames[1] Title

      .textFrames[2] Description

      .textFrames[3] Category

      and create a CSV containing a row for each page of the document.


      Here's where I'm stuck. (I can't even get a text file right)


      Thanks for your insights

        • 1. Re: Export Text by textFrame to a Single Text or CSV File
          cathykka Level 1

          Ok. I am in a really good place.

          I have figured out how to pull all the content I need on each page.

          But I cannot seem to figure out how to loop and write every page.

          As it stands right now, this script only outputs the last page.

          So close would appreciate any insights.

          Thanks.

           

          main ();

           

           

          function main() {

           

          var curDoc = app.activeDocument;

          var docPath = app.activeDocument.filePath; 

          var myPage = curDoc.pages.item(0);

          var myFrame = myPage.textFrames.everyItem(0).label;

          var myPageFrame = myPage.textFrames.item(0); 

          var myContents = myPage.textFrames.item(0).contents;   

           

          // create folder

          var folderPath = new Folder(docPath + "/" + curDoc.name.replace(/\.indd$/,"") );

              if (!folderPath.exists)

                  folderPath.create();

                 

          var filePath  = new File(folderPath + "/" + curDoc.name.replace(/\.indd$/,"")  + ".txt");

           

           

          for ( var i = 0; i < curDoc.pages.length; i++ ) {

              // the current page

              var curPage = curDoc.pages[i];

              // Page number

              var pName = curPage.name;

                  var section = curPage.textFrames.item("section").contents;

                  var category = curPage.textFrames.item("category").contents;

                  var description = curPage.textFrames.item("description").contents;

                  var description = curPage.textFrames.item("title").contents;

                  var keywords = curPage.textFrames.item("keywords").contents;

                      var pageMeta = curDoc.name.replace(/\.indd$/,"") + "-" + pName + "\t" + section + "\t" + category + "\t"+ description + "\t"+ title+ "\t" + keywords;

                     

          }

           

          // create file output

          var out;

                // we know already that the file exist

                // but to be sure

                if (filePath   !== '') {

                  //Open the file for writing.

                  out = filePath  .open('w', undefined, undefined);

                  filePath  .encoding = "UTF-8";

                  filePath  .lineFeed = "Unix"; //convert to UNIX lineFeed

                  // txtFile.lineFeed = "Windows";

                  // txtFile.lineFeed = "Macintosh";

                }

           

          var myMeta = (pageMeta);

           

          if (out !== false) {

          filePath.writeln(myMeta);

          filePath.close();

          }

                 

            } // end for > main

          • 2. Re: Export Text by textFrame to a Single Text or CSV File
            Kai Rübsamen Adobe Community Professional

            Try the following:

             

            main ();
            
            function main() {
                var curDoc = app.activeDocument;
                var docPath = curDoc.filePath; 
            
                // create a folder
                var theFolder = new Folder(docPath + "/" + curDoc.name.replace(/\.indd$/,"") );
                if (!theFolder.exists) {
                    theFolder.create();
                }
                // create a file
                var fileToWrite = new File(theFolder + "/" + curDoc.name.replace(/\.indd$/,"")  + ".txt");
                var ok = fileToWrite.open("w");
                    if (!ok) {
                        alert("Error: " + fileToWrite.error);
                        exit();
                }
                // collect the content and write it to the file
                for ( var i = 0; i < curDoc.pages.length; i++ ) {
                    // the current page
                    var curPage = curDoc.pages[i];
                    // page name
                    var pName = curPage.name;
                    var section = curPage.textFrames.item("section").contents;
                    var category = curPage.textFrames.item("category").contents;
                    var description = curPage.textFrames.item("description").contents;
                    var title = curPage.textFrames.item("title").contents;
                    var keywords = curPage.textFrames.item("keywords").contents;
                    var pageMetaString = curDoc.name.replace(/\.indd$/,"") + "-" + pName + "\t" + section + "\t" + category + "\t"+ description + "\t"+ title+ "\t" + keywords;
                    fileToWrite.writeln(pageMetaString + "\r");
                }
            
                fileToWrite.close();
            } // end main
            

             

            –Kai

            • 3. Re: Export Text by textFrame to a Single Text or CSV File
              cathykka Level 1

              Kai, it may be inappropriate... but I could kiss you. Thank You. You have no idea how long I tried to get to work. I am not a developer, code can be a challenge. I really appreciate you sharing your gift. Thanks. Oh by the way. It worked!!!!!

              • 4. Re: Export Text by textFrame to a Single Text or CSV File
                cathykka Level 1

                Ok this script worked like a charm and now. No Joy. Has something changed in recent upgrades?

                • 5. Re: Export Text by textFrame to a Single Text or CSV File
                  BSKTCreation Level 2

                  Hi Cathy,

                   

                  What error are you getting? Please provide as much info as possible.

                   

                  Brett

                  • 6. Re: Export Text by textFrame to a Single Text or CSV File
                    cathykka Level 1

                    Yes. That would be helpful wouldn't it.? Object is Invalid. Line 25 var section = curPage.textFrames.item("section").contents; 

                    • 7. Re: Export Text by textFrame to a Single Text or CSV File
                      BSKTCreation Level 2

                      Sorry for the delay I had to go to a very long meeting...

                       

                      This is a very common issue for CS5 and onward if you use script labels (which you are here).

                       

                      Its easily fixed by version controlling this line of code like so...

                       

                      var curVer = app.scriptPreferences.version;
                      app.scriptPreferences.version = "6.0";
                      var section = curPage.textFrames.item("section").contents;
                      // put your other text frame items here as well
                      app.scriptPreferences.version = curVer;
                      
                      
                      

                       

                      Let me know if this fixes it.

                      • 8. Re: Export Text by textFrame to a Single Text or CSV File
                        BSKTCreation Level 2

                        FYI, the other solution is to assign the textFrame to a variable as it is created. For example:

                         

                        var section = app.activeDocument.textFrames.add();
                        

                         

                        Then you just call the variable to do stuff.

                         

                        section.contents = "Some text.";
                        

                         

                        Of course this doesn't help much in a template scenario where the textFrames are already there waiting to be called. In that situation you need to version control the code or call its item number directly which is a pain, especially if you have 1,000s of the little buggers. This is the main reason I changed from template driven workflows to fully generated products a couple of years ago (they run way quicker too).

                         

                        Brett

                        • 9. Re: Export Text by textFrame to a Single Text or CSV File
                          cathykka Level 1

                          I appreciate whatever insight and time you can provide BSKT.... no apologies need. However I am still getting an error Error Number:45 Error String: Object is invalid Source: var section = curPage.textFrames.item("section").contents; Interpreter:CS4(6.0)

                          • 10. Re: Export Text by textFrame to a Single Text or CSV File
                            BSKTCreation Level 2

                            Do you have a text frame in your InDesign document with the script label "section"?

                             

                            The error you're getting suggests you don't.

                             

                            Brett

                            • 11. Re: Export Text by textFrame to a Single Text or CSV File
                              cathykka Level 1

                              I do. In my layers pallet on layer "Meta" there is a text box labeled "section" Under Window > Utilities > Script Layer it appears. That being said. I just thought of something. These TextBoxes are in the "SLUG" area of my document. (They have always been there and the script worked flawlessly before) I just tested moving the Text box onto the actual document and it worked. Any ideas why the slug area no longer works?

                              • 12. Re: Export Text by textFrame to a Single Text or CSV File
                                BSKTCreation Level 2

                                No idea, I use the slug area in my old templates any they still work...

                                 

                                Great work finding a solution though!