4 Replies Latest reply on Dec 22, 2015 7:31 AM by Iain Robinson

    [JS] need help on handling tables

    Iain Robinson Level 1

      Hi all

       

      Using CS5 on Mac and I have a problem I can't see the solution for. We have a Javascript we run on a job that has various snippets in it and some GREP search and replaces. One thing it does is gather up all the tables in the document and puts them in separate text boxes on the first page, ready to be placed at the appropriate place. This works well and is useful but some tables have notes following them. These are one or two paragraphs immediately following the table, which are followed by an empty para and then the start of the next table. Currently these are being left behind when the tables are moved.

       

      The code we are using to do this is;

      try
      {
      var myDoc = app.activeDocument;
      var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();
      
      
      for (var i=0; i<myTables.length;i++)
      {
              var tf = myDoc.pages[0].textFrames.add(
              {
              geometricBounds: ['6p', '6p', '36p', '36p'],
              });
      
      
      // stores the character holding the table as a variable
      var ip =  myTables[i].storyOffset.parent.characters[myTables[i].storyOffset.index];
      
      
      ip.move(LocationOptions.before, tf.insertionPoints[0]);
      }
      }
      catch (_) {}
      
      

       

      This obviously doesn't catch the paras between the tables and I'm not sure how I would gather up the tables (as a table object) and

      the paras (as text objects) together in a variable so I could start doing something with them. Not even sure what that would be yet - find one or more paras immediately following a table object and move that with the table?

       

      Any help appreciated.

       

      thanks,

      Iain

        • 1. Re: [JS] need help on handling tables
          Jump_Over Level 5

          Hi,

           

          So, going through, you need to detect the end of text which is going to be moved.

          (named cEndIP in below code)

          var
            myDoc = app.activeDocument,
            myIPs = myDoc.stories.everyItem().tables.everyItem().storyOffset,
            cStory, cStoryRest,
            cStartIP, cNextIP, cEndIP,
            tf, cText; 
          
          while (cStartIP = myIPs.pop() ) {
            cStory = cStartIP.parent;
            cNextIP = cStory.insertionPoints[cStartIP.index + 1];
            if (cNextIP == cStory.insertionPoints[-1]) cEndIP = cNextIP;
            else {
                 cStoryRest = cStory.insertionPoints.itemByRange(cNextIP.index, -1).texts[0];
                 cStoryRest = cStoryRest.tables.everyItem().storyOffset;
                 cEndIP = cStoryRest.length ? cStoryRest[0] : cStory.insertionPoints[-1];
                 }
            cText = cStory.characters.itemByRange(cStartIP, cEndIP).texts[0];
          
            tf = myDoc.pages[0].textFrames.add( 
                  { 
                  geometricBounds: ['6p', '6p', '36p', '36p'], 
                  });
          
            cText.move(LocationOptions.before, tf.insertionPoints[0]); 
            }
          

          Jarek

          • 2. Re: [JS] need help on handling tables
            Iain Robinson Level 1

            Thanks Jarek - that does it. I want to take the time to understand what each step is doing, not least because when I put it into my main script it crashed Indesign. I'm not familiar with, in particular, the use of ? and : in a line of code. Need to leave now but will come back to this tomorrow.

             

            thanks again,

            Iain

            • 3. Re: [JS] need help on handling tables
              Jump_Over Level 5

              Hi,

               

              cEndIP = cStoryRest.length ? cStoryRest[0] : cStory.insertionPoints[-1];
              
              

              is equal to:

              if (cStoryRest.length)
                  cEndIP = cStoryRest[0];
              else
                  cEndIP = cStory.insertionPoints[-1];
              
              

               

              Jarek

              • 4. Re: [JS] need help on handling tables
                Iain Robinson Level 1

                Thanks again Jarek. When I looked at it in more detail it wasn't your code that was making it crash it was something else entirely.

                 

                Iain