3 Replies Latest reply on Oct 11, 2013 2:44 PM by Jump_Over

    Generate TOC with JavaScript

    Dan-BTP

      I have a JavaScript script that opens an InDesign template and inserts text (including placing RTF files) on various pages throughout the template. Page 2 is empty, and I want to automatically generate a table of contents and place it on that page. When I use the following command, it always places it on page 1:

       

      doc.createTOC(doc.tocStyles.item("TOC"), true, null, ["1.0i", "1.0i"]);

       

      How do I get it on page 2? Thanks!

        • 1. Re: Generate TOC with JavaScript
          Jump_Over Level 5

          Hi,

           

          Move its text container (this is array of stories):

           

          //...
          var 
               doc = app.activeDocument,
               mTOC = doc.createTOC(doc.tocStyles.item("TOC"), true, null, ["1.0i", "1.0i"])[0].textContainers[0];
          
          mTOC.move(doc.pages[1]); 
          mTOC.move(["1.0i", "1.0i"]);
          //...
          

           

           

          Jarek

          1 person found this helpful
          • 2. Re: Generate TOC with JavaScript
            Dan-BTP Level 1

            Jarek,

             

            Thank you for the helpful suggestion, which I tried. It works, but I have two questions:

             

            1. I already have a text frame on page 2 that is set up for automatic reflow. If my TOC takes more than one page, how do I deal with that with the move technique? I would prefer to grab the story contents from the TOC and put them in the text frame on page 2. Is this possible?

             

            2. The main body of the document ends up with 28 pages of connected text. When I omit the createTOC step, the document flows properly and I have no overset text. When I include the createTOC (whether I leave it on page 1 or move it to page 2), the automatic reflow stops about three pages before the end of the document and gives me an overset text error. The only code difference in these two scenarios is whether or not I comment out the createTOC statement. Any idea why it messes with the automatic reflow of the body text?

             

            Dan

            • 3. Re: Generate TOC with JavaScript
              Jump_Over Level 5

              Hi,

               

              1. I already have a text frame on page 2...

               

              While creating TOC its textFrame is marked so TOC story could be updated easy. If you move a story - not a text Frame - this connection is broken.

              None the less you can use a move method to a story as well. (see http://jongware.mit.edu/idcs5js/pc_Story.html#move)

               

              2 ... the automatic reflow stops about three pages before the end of the document and gives me an overset text error....

               

              Hard to say anything with no look at the code...

               

              Jarek