8 Replies Latest reply on Nov 14, 2009 9:39 AM by [Jongware]

    create TOC javascript for indesign book


      Hi friends,


      i am new to indesign javascript. i want to create a javascript for TOC indesign book using paragrph style


      could anyone help me. i am strugling long time.

        • 1. Re: create TOC javascript for indesign book
          [Jongware] Most Valuable Participant

          That sounds like the regular TOC function (it also works on Books).


          Do you want to have a script that does the same without using the built-in TOC function (which is ... well, not really necessary, is it?), or do you want to use the TOC function through Javascript? (See in the ESTK Help: TOCStyle, TOCStyleEntry for the definitions, and Document.createTOC to actually create it.)

          • 2. Re: create TOC javascript for indesign book
            msankar.ravi Level 1

            Thank you very much Jongware for your reply. Actualy i tried predefined function createTOC(). but it did'nt wotk for me.


            i want to do in without buildin function .

            • 3. Re: create TOC javascript for indesign book
              Marc Autret Level 4

              Maybe IndexMatic could help you in this task.


              You can download the script from this page:





              • 4. Re: create TOC javascript for indesign book
                [Jongware] Most Valuable Participant

                "It didn't work for me" doesn't work for me. Always, always, always, state why it doesn't work for you.


                Did you try to use it, but could not get it to work?

                Or, do you need to do something special that the built-in function does not do?


                It should not be extremely hard to make a javascript version that does (roughly) the same. You could try something like this:


                1. For every paragraph style you want to include (most likely all sorts of headings),
                2. Search for all occurrences, and store the text, its page number, and (important!) its story offset in an array.
                3. You need the text because that's what will appear in the TOC.
                4. You need the page number because, well, that's what the TOC is for :-)
                5. Most important: you need the story offset so you can reconstitute the original order of the headings. You can only search for one level of heading at a time, so without sorting, you would get "Heading 1 -- Heading 1 -- Heading 1 -- Heading 2 -- Heading 2 -- Heading 2" (etc.)
                6. When you have gathered all of the information for your entire book, sort on story offset. (I forgot, it's for a Book, right? You need to store a document id as well, so you can sort throughout multiple documents.) (See Ed.)
                7. Then write out every text in the right order, followed by its page number.


                The IndexMatic script that Marc points out should already contains lots of the logic described here -- esp. searching and storing page numbers.


                [Ed.] Thinking about it, a Book (multiple documents) should *not* cause any problems, and you don't need to know in which document some text was found. Simply sort the paragraphs on their page number; only when these are the same for more than one paragraph, sort just these on their Story Offset. Logically, there should not be two headers with the same page number in two different documents.

                • 5. Re: create TOC javascript for indesign book
                  rpollack-2 Level 1

                  I am wondering if you need a script. The TOC feature in InDesign CS4 seems pretty easy to use. I think it takes a bit of understanding how the TOC feature works.


                  I'm relatively new using ID CS4, however I use the built-in feasture all the time. Let me know if you use the TOC feature (in the Layou menu).



                  • 6. Re: create TOC javascript for indesign book
                    msankar.ravi Level 1

                    can you tell me how to use. i tried buildin createTOC(). i am getting confused with parameter. it's worked for me . but TOC placed when i go to Layout-->


                    table of contents  and if i click the TOC style-->paragraph style. The TOC place into the 4th page. why it's not working in this code i don't know.


                    here i place the code.


                    function toc() {
                    //    Set MyTOCStyle = myDoc.TOCStyles.Add
                        //Set myPage = myDocument.Pages.Item(1)
                        var myTOCStyle = myDocument.tocStyles.itemByName("TOC");
                        var myTocPage = myDocument.pages.add(LocationOptions.AT_END);
                        var myBounds = myGetBounds(myDocument, myTocPage);
                        var myX1 = myBounds[1];
                        var myY1 = myBounds[0];
                        var myStory = myDocument.createTOC(myTOCStyle, true, undefined, [myX1, myY1]);
                        var myFrame = myStory.textContainers[0];





                    • 7. Re: create TOC javascript for indesign book
                      msankar.ravi Level 1

                      Hi Jongware,


                      with your valuable help i created TOC using predefined function. i defined the TOC style created from Layout---->Table of contenet--->TOCStyle.



                      but i want to create through script. can u tell me the syntax.

                      • 8. Re: create TOC javascript for indesign book
                        [Jongware] Most Valuable Participant

                        The syntax is


                        Array of Story createTOC (using: TOCStyle[, replacing: bool=false][, fromBook: Book][, placePoint: Array of Measurement Unit (Number or String)=any][, includeOverset: bool=false][, destinationLayer: Layer])
                        Creates a table of contents.

                        usingTOCStyleThe TOC style to use to define the content, title, and format of the table of contents.
                        replacingboolIf true, replaces the existing TOC. (Optional) (default: false) (Optional)
                        fromBookBookThe book whose documents to include in the TOC. (Optional)
                        placePointArray of Measurement Unit (Number or String)=anyThe point at which to place the TOC story, specified as page coordinates in the format [x, y]. (Optional)
                        includeOversetboolIf true, includes overset text TOC entries in the TOC. (Optional) (default: false) (Optional)
                        destinationLayerLayerThe layer on which to place the TOC. (Optional)


                        -- and I copied it from the ESTK Help, which is, really, the best place to look up the syntax for any JS command ...