5 Replies Latest reply on Apr 20, 2012 7:04 PM by Brian.T.Tanner

    Trouble updating TOC with Javascript


      Using: Indesign 5.5 - javascript



      I'm trying to simply force a toc that's already placed on a master page to update.


      Upon searching it looks like using the createTOC() method should help accomplish this, but the ESTK gets stuck at the createTOC() line, saying simply "createTOC".


      Here's what I'm using so far:




      var myTOCstyle = document.tocStyles.itemByName("townTOC");


      document.createTOC(myTOCstyle, true);




      Does anyone have any suggestions?


      Thanks much.


        • 1. Re: Trouble updating TOC with Javascript
          Laubender Adobe Community Professional & MVP

          @Nate – As always in cases like this when no method seems to match a thing you can do in the UI, I'm looking for a way to accomplish it through script menu actions.


          There might be a better method, but one always could try to invoke the appropriate script menu action:


          //SELECT the text frame that is holding your table of contents (TOC):
          myTOCtextFrame.select(); //You have to define "myTOCtextFrame" first!
          //Update Table of Contents by script menu action:
          //Deselect selection of text frame holding your TOC: 




          Message was edited by: Laubender

          1 person found this helpful
          • 2. Re: Trouble updating TOC with Javascript
            rafikibubu Level 1



            This is great, thank you. It does indeed accomplish what I need for the time being. However, I do remain curious of alternate methods. Particularly, I'd love to figure out why my original code gets stuck. With everything I've been reading, for the life of me, I can't figure out the reason for the jam.


            But again, this is a way, for now. So thank you ever so much.


            Might I pose a corollary question?


            It's indeed handy to know how to invoke a menu action. As you'd mentioned, this stuff will inevitably come up from time to time. How does one look up a particular menu action's ID?




            • 3. Re: Trouble updating TOC with Javascript
              Laubender Adobe Community Professional & MVP

              @Nate – How does one look up a special menu action?


              For example see Jongware's method:




              In this case it's the "name" property. You can also look for "id" and get a handy table of names and IDs together.


              If you search this forum you will see other methods for listing menu actions…



              1 person found this helpful
              • 4. Re: Trouble updating TOC with Javascript
                rafikibubu Level 1



                Very cool, thanks so much again. I'll keep that noted. I'm sure it'll come in handy for alot of the tasks I'm trying to accomplish.




                • 5. Re: Trouble updating TOC with Javascript
                  Brian.T.Tanner Level 1

                  I have had mixed success with attempting the same thing and honestly the problem you are having is one that has made me pull quite a bit of my hair out.


                  I have a tip for you.


                  Look into your TOC style called "townTOC".  Is "Include Book Documents" on or off? 


                  In my case, it was on, because the document was at one point part of a book.  After taking it out of the book, I could not revert this to off without reloading the style from another file (or adding it back to a book).


                  Anyway, it seems like, if: "Include Book Documents" is On AND the Document is NOT in a Book, then document.createTOC will fail.


                  But wait, there is a weird fix too.  I presume possibly you are doing things like me and you have not attached a window to your document because things work faster if the document is not visible. This error only seems to happen when the document has no window attached. Try this code and bang your head against the wall if it works.



                       document.createTOC(myTOCstyle, true);



                       document.createTOC(myTOCstyle, true);

                       $.writeln("Seriously? Why does this work!");



                  So, for me, I first learned of the second method, and was unsatisfied but at least my code didn't crash.  Now I have fixed the style to not have book contents and it seems to work.  Obviously there are a lot of specific circumstances at play so it would be very valuable if you could report back how this fits your situation and if any of this is helpful.