0 Replies Latest reply on Feb 29, 2012 9:47 AM by rafikibubu

    Finished script: Use grep find/change to fill in a supplied table of contents

    rafikibubu

      This script is now complete, and has been the subject of most of my previous posts. Just in case anyone wanted to know what the finished script ended as, here it is.

       

      Thanks so much to all. A lot of really helpful folks on this board are very responsible for the success of this task. This script is to be one of hopefully many in the creation of our records. But it's a huge leap forward. Thanks again to everyone that helped.

       

      Cheers,

      ~Nate

       

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

      Task:

       

      Automatically find town names in listings, and fill in table of contents template on page 2 accordingly.

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

       

      Example of page 2 toc, initially:

       

      Picture 4.png

       

       

      Example of a page of content. The town names are what need to be referenced on the TOC:

       

      Picture 5.png

       

      Example of page 2 toc once script is finished:

       

      Picture 6.png

       

       

      Because of the need to include the transaction dates on the TOC (comes as a provided, tagged-text file), a simple Indesign-generated TOC can't be used alone.

       

      This script uses an Indesign-generated TOC that's on a master page called "T-tocGen" ... It then uses grep search and replaces to grab the needed information, and insert it into the page 2 TOC.

       

      The script will update a generated TOC and then search for an instance of a page number, and town name. The generated toc lists all included towns in the following format:

       

      (line start)## tab townName(line end)

       

      In Grep, this would be (please note, extra \ for \d and \t ... javascript needs that for some reason):

       

      ^\\d+\\t(.*)$

       

      After the script gets the info it needs from a found instance of the above, it replaces that line with "---", to prevent that line from being picked up once again.

       

      The script with then place the needed page number in it's rightful place on page 2, replacing the XX.

       

      A while loop is used to repeat the above process until there are no longer any instances of "^\\d+\\t(.*)$" present.

       

      Not every town runs every issue, so once the script is done, it removes all remaining instance of "XX" on the page 2 TOC.

       

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

       

      FINAL CODE:

       

       

      TOC replace

      /*

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

      This script will use grep find/change methods to apply page numbers in

      tocGen to the XX's on page2TOC.

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

      */

       

       

      // define the text frame of generated TOC

          var tocGenFrame  = document.masterSpreads.item("T-tocGen").pages.item(0).textFrames.item(0);

         

      // udpate generated TOC ... store contents in tocGenStuff

          var tocGenStuff = updateTOCGen();

       

       

      // set variable for while loop

      var okGo = "1";

       

       

       

       

      // while okGo isn't 0

      while(okGo.length!=0)

      {

       

       

      // get town info from tocGen

      getCurrentTown();

       

       

      // replace XX's with tocGen info

      replaceTown();

       

       

      // grep find ... any remaining towns with page numbers in tocGen?

      app.findGrepPreferences = app.changeGrepPreferences = null;

      app.findGrepPreferences.findWhat = "^\\d+\\t(.*)$";

       

       

      // set current value of okGo ... with any instances of above grep find in tocGen

      okGo = tocGenFrame.findGrep();   

      }

                   

       

       

       

       

      // grep find/change all leftover XXs in page2TOC

      app.findGrepPreferences = app.changeGrepPreferences = null;       

      app.findGrepPreferences.findWhat = "^XX\\t";

      app.changeGrepPreferences.changeTo = "\\t";

      app.activeDocument.changeGrep();  

       

       

      // clear grep prefs

      app.findGrepPreferences = app.changeGrepPreferences = null;

       

       

       

       

       

       

      ////////////////////////

      //  functions                  //

      ////////////////////////

       

       

      function getCurrentTown()

      {

         

      // grep options   

      app.findChangeGrepOptions.includeLockedLayersForFind = true;

      app.findChangeGrepOptions.includeLockedStoriesForFind = true;

      app.findChangeGrepOptions.includeHiddenLayers = true;

      app.findChangeGrepOptions.includeMasterPages = true;

      app.findChangeGrepOptions.includeFootnotes = true;

       

      // grep find:  startLine anyDigits tab anyCharacters endLine

            app.findGrepPreferences = app.changeGrepPreferences = null;

            app.findGrepPreferences.findWhat = "^\\d+\\t(.*)$";

            

      // get grep find results      

      currentGen = tocGenFrame.findGrep();  

       

       

      // store grep results content into currentLine

      currentLine = currentGen[0].contents;

       

       

      // match to get array of grep found items

      currentMatch = currentGen[0].contents.match("^\\d+\\t(.*)$");

       

       

      // second found item is town name, store as currentTown

      currentTown = currentMatch[1];

       

       

      // change current line to --- now that data has been grabbed

      // this is because loop will continue as long as the above grep find yields a result

             app.findGrepPreferences.findWhat = "^\\d+\\t"+currentTown+"$";

                    app.changeGrepPreferences.changeTo = "---";

                  tocGenFrame.changeGrep(); 

      }

       

       

       

       

       

       

      function replaceTown()

      {

       

      app.findChangeGrepOptions.includeLockedLayersForFind = true;

      app.findChangeGrepOptions.includeLockedStoriesForFind = true;

      app.findChangeGrepOptions.includeHiddenLayers = true;

      app.findChangeGrepOptions.includeMasterPages = true;

      app.findChangeGrepOptions.includeFootnotes = true;

       

      // find: XX currentTown .... replace with: currentLine

          app.findGrepPreferences = app.changeGrepPreferences = null;

          app.findGrepPreferences.findWhat = "^XX\\t"+currentTown+" \\(";

          app.changeGrepPreferences.changeTo = currentLine+" \(";

            

      app.activeDocument.changeGrep();   

      }

       

       

       

       

       

       

      function updateTOCGen()

      {

         

      //set vars ... toc text frame, toc master pag

          var tocGen  = document.masterSpreads.item("T-tocGen").pages.item(0).textFrames.item(0);

          var tocGenPage  = document.masterSpreads.item("T-tocGen").pages.item(0);

       

       

      //SELECT the text frame generatedTOC on the master TOC

          tocGen.select();

       

       

      //Update Table of Contents by script menu action:

          app.scriptMenuActions.itemByID(71442).invoke();

       

       

      //Deselect selection of text frame holding your TOC:

          app.select(null);

       

       

      //store contents of toc text frame in variable

          var tocGenText = tocGen.contents;

       

       

      //return contents of tocGen

          return tocGenText;

         

      }