8 Replies Latest reply on Dec 2, 2013 10:04 AM by Izac R...

    Create a TOC in an actual table?

    Izac R...

      http://forums.adobe.com/thread/1345217

       

      This was discussed in the above thread. A "Clunky" and not very user friendly way was found to accomplish the end result in this instance. However in trying to make the process as automated as possible the concluded solution was at least a few steps that really feel like they shouldn't be necessary to achieve this seemingly simple task. So I got suggested to ask the scripting side of the spectrum and that's exactly what i'm doing! I admittedly don't know much to even anything about scripting. I've tried learning a few languages but could never really catch on and that was a while ago. i hate asking for handouts so please dont feel obligated to just write me up a script. However if anyone has some free time or just finds this dilemma as something easy/challengeing(whatever you're into) or even benefical to themselves to write a script for it would be greatly appreciated.

       

      Once again deatails of the situation can be found in the above thread, Thanks!

       

      *Currently using CS6

        • 1. Re: Create a TOC in an actual table?
          Jump_Over Level 5

          Hi,

           

          I tested this on a simple doc&toc, so not sure if it will pass your circumstances.

           

          Code updates TOC (so it should be created already), converts to a table and assumes:

          • TOC use doc.tocStyles.item(0) ==> modify if needed 1st line
          • a tableStyle "TOC" is defined (alternative fill and head cellStyle applied)
          • a cellStyle "TOC_Head" is defined

           

          var
                    mTOCstyle = app.activeDocument.tocStyles.item(0),
                    mTableStyle = app.activeDocument.tableStyles.item("TOC"),
                    mHeadStyle = app.activeDocument.cellStyles.item("TOC_Head"),
                    mStory = app.activeDocument.createTOC(mTOCstyle, true),
                    mT = mStory[0].texts[0].convertToTable();
          
          mT.headerRowCount = 1;
          mT.breakHeaders = HeaderFooterBreakTypes.IN_ALL_TEXT_COLUMNS;
          mT.rows[0].merge();
          mT.rows[1].cells[0].texts[0].move(LocationOptions.AFTER, mT.cells[0].texts[0]);
          mT.rows[1].remove();
          mT.appliedTableStyle = mTableStyle;
          mT.cells[0].clearCellStyleOverrides(true);
          

           

          just a raw idea...

          try it

           

          Jarek

          1 person found this helpful
          • 2. Re: Create a TOC in an actual table?
            Izac R... Level 1

            Capture.PNG

             

            var

                      mTOCstyle = app.activeDocument.tocStyles.item("TABLE OF CONTENTS"),

                      mTableStyle = app.activeDocument.tableStyles.item("TOC Table Style For IPL's & OPM's"),

                      mHeadStyle = app.activeDocument.cellStyles.item("TOC HEADER"),

                      mStory = app.activeDocument.createTOC(mTOCstyle, true),

                      mT = mStory[0].texts[0].convertToTable();

             

            mT.headerRowCount = 1;

            mT.breakHeaders = HeaderFooterBreakTypes.IN_ALL_TEXT_COLUMNS;

            mT.rows[0].merge();

            mT.rows[1].cells[0].texts[0].move(LocationOptions.AFTER, mT.cells[0].texts[0]);

            mT.rows[1].remove();

            mT.appliedTableStyle = mTableStyle;

            mT.cells[0].clearCellStyleOverrides(true);

             

             

            First off thanks a bunch. I got this far by editing the script you gave me to match my style names and the above picture is a result of runing the script. It's really close to the way I would like it to drop in (Aside from the styles I used not being what I want them to look like yet, but that's my problem). Although it would be great if there was a way in the script to make the second column fit to the biggest line of text in it. That way in theory there would be no rows with two lines of text in the first column (as long as no text in any row on column 1 was super long [which I can change if needed]). Thanks again.

            • 3. Re: Create a TOC in an actual table?
              Jump_Over Level 5

              Hi,

               

              To fit column to its content means to find text.horizontalOffset vs. text.endHorizontalOffset max difference.

               

              So your code could look like:

              var
                   mTOCstyle = app.activeDocument.tocStyles.item("TABLE OF CONTENTS"),
                   mTableStyle = app.activeDocument.tableStyles.item("TOC Table Style For IPL's & OPM's"),
                   mHeadStyle = app.activeDocument.cellStyles.item("TOC HEADER"),
                   mStory = app.activeDocument.createTOC(mTOCstyle, true),
                   mT = mStory[0].texts[0].convertToTable();
              
              mT.headerRowCount = 1;
              mT.breakHeaders = HeaderFooterBreakTypes.IN_ALL_TEXT_COLUMNS;
              mT.rows[0].merge();
              mT.rows[1].cells[0].texts[0].move(LocationOptions.AFTER, mT.cells[0].texts[0]);
              mT.rows[1].remove();
              mT.appliedTableStyle = mTableStyle;
              mT.cells[0].clearCellStyleOverrides(true);
              
              var 
                   myC = myT.columns[-1],
                   myHori = myC.cells.everyItem().texts[0].horizontalOffset,
                   myEndHori = myC.cells.everyItem().texts[0].endHorizontalOffset,
                   myTabWidth = myT.width, myCurrWidth = myC.width, myTarWidth,
                   myIndent = 5;
              
              myHori.sort(sortNum);
              myEndHori.sort(sortNum).reverse();
              myTarWidth = myEndHori[0] - myHori[0] + myIndent;
              myT.columns[0].width = myTabWidth - myTarWidth;
              myC.width = myTarWidth;
              
              
              function sortNum (a,b) {
                   return parseFloat(a) >= parseFloat(b);
                   }
              

               

              there is some indent added to column's width by default. I set it to 5.

               

              Jarek

              1 person found this helpful
              • 4. Re: Create a TOC in an actual table?
                Izac R... Level 1

                Hello thanks for the quick responses. When i run the last script version you sent it builds the table same as last time but doesn't resize columns and gives this error:

                Capture.PNG

                I thought maybe "myT." was put in by mistake instead of "mT." (Which was used in the first part of the script, but replacing it gave a kind of an opposite result from the one intended.:

                 

                Capture2.PNG

                 

                Sorry i'm kind of hopeless when it comes to scripting.

                • 5. Re: Create a TOC in an actual table?
                  Jump_Over Level 5

                  Hi,

                   

                  In case of "myT" - you are right - that's my fault, sorry.

                  In case of opposite width effect... hm... is your left page measurements growing from right to left?

                   

                  To ensure zeroPoint at left-top corner include:

                   

                  app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
                  app.activeDocument.zeroPoint = [0,0];
                  

                   

                  Jarek

                  1 person found this helpful
                  • 6. Re: Create a TOC in an actual table?
                    Izac R... Level 1

                    Capture.PNG

                    Still nothing. Did i put those last two lines you suggested in the wrong spot?

                    • 7. Re: Create a TOC in an actual table?
                      Jump_Over Level 5

                      Hi,

                       

                      Notice indent = 5

                      With your measurement unit (inch) it is pretty big.

                      Modify myIndent value.

                       

                      Jarek

                      • 8. Re: Create a TOC in an actual table?
                        Izac R... Level 1

                        AHHH im silly i didnt even consider that! well all is well now thanks a bunch for all your help! Capture.PNG