4 Replies Latest reply on Sep 18, 2012 12:09 AM by Miguel Otz

    Search refereces by grep and get page in file text

    Miguel Otz

      I have a indesign files with texts and tables in 20 pages. The document is a references catalog. I need to get all reference and in what page there are. All result save in a text file.

      I think that the best mode is looking a reference with grep, but i don´t know how to make with  tables and get the content.

      Please, could you help me?

      The structure of references is /d/d/d/./d/d/d/./d/d/d

      When i run this script in only textframe go on, but if i have table, give me attachment error

      Thank very much.

      Bye

      Miguel

      Attachment error:

      Captura de pantalla 2012-09-17 a la(s) 08.07.32.png

       

      Attachment Script:

       

      myDoc = app.activeDocument;

      log1 = makeLogFile("/Users/mcolmenero/Desktop/","test",myDoc,true);

      var myDocument = app.activeDocument;

      var myPages=myDocument.pages;

      var myRange=[];

      // clear find change text preferences before search

      app.findGrepPreferences = NothingEnum.NOTHING;

      app.changeGrepPreferences = NothingEnum.NOTHING;

      app.findGrepPreferences.findWhat = "(\\d\\d\\d)(\.)(\\d\\d\\d)(\.)(\\d\\d\\d)";

       

      var myNumPages = app.activeDocument.pages.length;

      var texto="";

       

      for (var i=0; i<myNumPages;i++){

           myRange = myRange.concat(myDocument.pages.item(i).textFrames.everyItem().getElements());

          var formatGrep=theGrepFinder("(\\d\\d\\d)(\.)(\\d\\d\\d)(\.)(\\d\\d\\d)",myRange);

          formatGrep.constructor.name;

          var num=formatGrep.length;

          if (num>0){

          var valor=formatGrep[0].contents;

          texto=texto+ "valor "+valor+" \tpag "+i+"\n";

          }

      }

       

      log(log1, texto);

      log1.execute();

       

       

       

          // Funciones varias

      function theGrepFinder(grepFindIt,theRange){

          app.findGrepPreferences = NothingEnum.NOTHING;

          app.changeGrepPreferences = NothingEnum.NOTHING;

          app.findGrepPreferences.findWhat = grepFindIt;

          var retVal;

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

              retVal =(theRange[i].findGrep());

          }   

      return retVal;

      }//end theGrepFinder

       

       

      function makeLogFile(ruta,aName, aDoc, deleteIt) {

        var logLoc=ruta; // path to folder that will hold log file

        try {

          logLoc = aDoc.filePath;

        } catch (e) {

          logLoc = getScriptPath().parent.fsName

        }

        var aFile = File(logLoc + "/" + aName + ".txt");

        if (deleteIt) {

          aFile.remove();

          return aFile;

        }

        var n = 1;

        while (aFile.exists) {

          aFile = File(logLoc + "/" + aName + String(n) + ".txt");

          n++

        }

        return aFile

      }

       

      function log(aFile, message) {

        var today = new Date();

        if (!aFile.exists) {

          // make new log file

          aFile.open("w");

          aFile.write(String(today) + "\nLas referencias encontradas son:\n");

          aFile.close();

        }

        aFile.open("e");

        aFile.seek(0,2);

        aFile.write("\n" + message);

        aFile.close();

      }

        • 1. Re: Search refereces by grep and get page in file text
          thor2112

          Hi Miguel,

           

          Would something like this work?  I also added ^ and $ to the regex to be more exact, which I don't know if you'll want.  I've never worked with tables before - do they always exist in textFrames?  If so, I'm assuming you could just loop through each textFrame.

           

          --------------------------------------------

          var doc = app.activeDocument;

          var pageArray = doc.pages;

           

          var txt = '';

           

          app.findGrepPreferences = NothingEnum.nothing;

          app.changeGrepPreferences = NothingEnum.nothing;

           

          app.findGrepPreferences.findWhat = '^\\d{3}\\.\\d{3}\\.\\d{3}$';

           

          for (var p = 0; p < pageArray.length; p++) {

              var pg = pageArray[p];

             

              var textFrameArray = pg.textFrames;

             

              for (var t = 0; t < textFrameArray.length; t++) {

                  var rangeArray = textFrameArray[t].parentStory.findGrep();

             

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

                      txt += ('Page: ' + (p + 1) + '   Reference: ' + rangeArray[i].contents + '\n');

                  }

              }

          }

           

          app.findGrepPreferences = NothingEnum.nothing;

          app.changeGrepPreferences = NothingEnum.nothing;

           

          $.writeln(txt);  // Return result to console in ExtendScript

          --------------------------------------------

          /* Result

          Page: 1   Reference: 222.333.444

          Page: 1   Reference: 111.222.333

          Page: 2   Reference: 222.222.222

          Page: 2   Reference: 777.222.332

          Page: 2   Reference: 777.222.333

           

          */

          • 2. Re: Search refereces by grep and get page in file text
            Peter Kahrel Adobe Community Professional & MVP

            It can be much easier. Try this one:

             

            app.findGrepPreferences = null;

            app.findGrepPreferences.findWhat = '\\d{3}\\.\\d{3}\\.\\d{3}';

            f = app.documents[0].findGrep();

            for (i = 0; i < f.length; i++)

                $.writeln (f[i].contents + ' on page ' + f[i].parentTextFrames[0].parentPage.name);

             

            The parentPage propert was introduced in CS5. And maybe the $.writeln () should go in a try/catch construct just in case. For CS4 and earlier, which don't know parentPage, use athe findPage function, of which there are sevral versions in this forum.

             

            Peter

            • 3. Re: Search refereces by grep and get page in file text
              thor2112 Level 1

              Very nice!  Thanks Peter!  :-)

              • 4. Re: Search refereces by grep and get page in file text
                Miguel Otz Level 1

                Thank Peter and thor2112. It's go on, That's good!

                Bye

                Miguel Colmenero