1 Reply Latest reply on Feb 12, 2013 7:48 AM by Laubender

    How to update only active TOC Styles of document

    Erkki Pennanen

      Hi,

       

      The code below works fine (is updating all TOCs) if the TOC Style is used, but it will create new TOCs if they are not used. I´d like to create a generic script which should only update the active TOCs. Is there any method to find if the TOC Style is not in use?

       

      Thanks in advance?

       

      Erkki

       

      ------

       

      try{

      var i,

          t = app.activeDocument.tocStyles.length;

                alert("This document contains " + t + " table of contents, which will be updated.");

      for (i=0; i<t; i++) {

                var myTOCStyle = app.activeDocument.tocStyles.item(i);

        app.activeDocument.createTOC(myTOCStyle,true);

      }

      alert("All TOCs have been updated, thanks!")

      }catch(e){alert(e)};

        • 1. Re: How to update only active TOC Styles of document
          Laubender Adobe Community Professional & MVP

          @Erkki – it's an old thread and not answered yet…

           

          Currently I'm working on TOCs and came accross a similar problem.

           

          One idea is based on the facts, that:
          1. If you update with an unused tocStyle a NEW text frame is added to the textFrames collection of the document

          2. If you update a already used tocStyles no new text frame is added

          3. We could identify a new text frame and delete it after it is generated

           

          Here a simplified example:

           

          //ASSUMPTION: All text frames in the document are unlocked! No locked layers! No hidden layers!
          
          var d=app.documents[0];
          
          for(var n=0;n<d.tocStyles.length;n++){
          
              //Let's check the length of the text frames collection in the document
              //and store its value for further use:
              var textFramesLength = d.textFrames.length;
          
              //NEXT: we will update a single TOC by using the createTOC() method
              //1. argument is set to a single tocStyle
              //2. argument is set to "true" (= just update an existing TOC)
          
              //However:
              //An UNUSED tocStyle will always generate a NEW text frame!
              //An already used tocStyle will NOT generate a NEW text frame!
          
          //(What will happen, if the text frame is locked? Or hidden? Should we care for that?)
          //(Let's assume for simplicity that no text frame in the document is locked! And no layer is locked!)
          
              //createTOC() will return an Array of Stories.
              //Its length = 1, because we use one single tocStyle every time we loop.
          
              //We store the Story object of the just updated (or new) TOC in a variable:
          
              var tocStory = d.createTOC(d.tocStyles[n],true);
          
              //We have access to the text frame of that Story Object
              //by its textContainer:
              var parentTextFrameOfToc = tocStory[0].textContainers[0]; 
          
              //After updating (or generating) a TOC we check, if the text frames collection of the document 
              //is still the same length as before.
          
              //If NOT, we can remove the text frame of the recently created TOC:
              if(d.textFrames.length != textFramesLength){
                  parentTextFrameOfToc.remove();
                  };
          
              };
          

           

           

          Of course you can add a counter to the loop and report later, how many TOCs were updated.

           

          Hope, that helps!

           

           

          Uwe

           

          Message was edited by: Laubender (typo in the script's comments)