4 Replies Latest reply on Jan 27, 2011 2:04 AM by Roy Marshall

    Distribute tabbed table equally

    Shonkyin Level 3

      Hi All,

       

      I want a script that can distribute tabbed table equally.

       

      Please see my below code:

       

       

      if (app.selection[0].constructor.name != "Text") exit ();

      var myParagraphs = app.selection[0].paragraphs;

      var longest = 0;

      var para = []

       

      for(b=0;b<myParagraphs.length; b++)

      {

      para[b] = ((myParagraphs[b].contents).replace(/\r/,"")).split(/\t/)

      }

       

      var longest = new Array();

      longest[0] = 0;

      longest[1] = 0;

      longest[2] = 0;

      longest[3] = 0;

      longest[4] = 0;

       

       

      for (a=0; a<5; a++)

      {

      for(b=0;b<myParagraphs.length;b++)

      {

      longest[a] = Math.max(parseInt(longest[a]),para[b][a].length)

      }

      }

       

       

      var textframebound=app.selection[0].parentTextFrames[0].geometricBounds;

       

      var myHeight = textframebound[2] - textframebound[0];

      var myWidth = textframebound[3] - textframebound[1];

      var totalcolWidth = 0

      for (a=0; a<longest.length; a++)

      {

      totalcolWidth += longest[a]

      }

       

      var distribut = myWidth - totalcolWidth

      In above code i got the longest value in each column and now i am confused how can i get point size width and set stop tab on each column.
      Thanks
      Shonky

        • 1. Re: Distribute tabbed table equally
          [Jongware] Most Valuable Participant

          Don't use this:

           

          longest[a] = Math.max(parseInt(longest[a]),para[b][a].length)

           

          as it returns the length in characters. To get the real width, subtract the Horizontal Offset of the first character from the one of the last character:

           

          alert (app.selection[0].paragraphs[0].insertionPoints[-2].horizontalOffset - app.selection[0].paragraphs[0].insertionPoints[0].horizontalOffset);

           

          That will give you the exact width of the text inbetween.

          Note that you converted your text to a plain string before (reading 'contents'), so you can't plug this line in right away. Measuring works on the Words array of a paragraph (and on any Character range as well), so all you have to do is getting a handle to the first and last character of each number and measure the distance inbetween.

           

          ---

           

          The second part, handling tabs, is done through the paragraph's TabStops property. Removing all tabs:

           

          tabStops.everyItem().remove();

           

          Adding a tab:

           

          tabStops.add ({position:SomePosition});

          1 person found this helpful
          • 2. Re: Distribute tabbed table equally
            Shonkyin Level 3

            Hi Jongware,

             

            Thank you for your response,

             

            longest[a] = Math.max(parseInt(longest[a]),para[b][a].length)

            with above code i am trying to getting maximum characters length in each column and then i'll multiply with point size width.
            But with your code:

            app.selection[0].paragraphs[0].insertionPoints[-2].horizontalOffset - app.selection[0].paragraphs[0].insertionPoints[0].horizontalOffset

             

            it give me whole paragraph width not each column.

             

            How can i get each column width with your code please suggest.

             

            coding for tab setting now clear, thanks again.

             

            Shonky

            • 3. Re: Distribute tabbed table equally
              Shonkyin Level 3

              Hi All,

               

              help me to complete this script.

               

              Shonky

              • 4. Re: Distribute tabbed table equally
                Roy Marshall Level 1

                Hi.

                 

                I have also been working on this feature, and have come up with the following.

                It works fine in CS5, but I have not tested elsewhere. I have not commented yet, as I have just finished.  Maybe you have sorted this already?

                The following script has no extensive error catching and reporting, and will work on a text frame, that has multiple lines, the same amount of tabs per line, and the text frame large enough to hold the text when the new tabs are applied.

                Anyway, here is the script:

                 

                try{
                myStart = true;
                myDoc = app.documents[0];
                myFrame = myDoc.selection[0];
                myStory = myFrame.paragraphs[0].parentStory;
                myLineCount = myStory.lines.length;
                myControlArray = new Array();
                for(i=0;i<myLineCount;i++){
                    myLine = myStory.paragraphs[i];
                    myReset();
                    app.findTextPreferences.findWhat = "^t";
                    myTabFind = myLine.findText();
                    myReset();
                    myTabLength = myTabFind.length;
                    if(myStart == true){
                        for(n=0;n<myTabLength;n++){
                            myControlArray[n] = 0;
                            }
                        }
                    for(n=0;n<myTabLength;n++){
                        myStart = false;
                        // Finding the distance of each tab
                        myIndex = myTabFind[n].index;
                        if(n==0){
                            if((myStory.characters[myIndex].horizontalOffset)-myStory.characters[0].horizontalOffset > myControlArray[n] ){
                                myControlArray[n] = ((myStory.characters[myIndex].horizontalOffset) - myStory.characters[0].horizontalOffset) + 4;
                                }
                            }
                         else{
                             myEndPoint = myStory.characters[myTabFind[n].index].horizontalOffset;
                             myStartPoint = myStory.characters[(myTabFind[n-1].index)+1].insertionPoints[0].horizontalOffset;
                             if(myEndPoint - myStartPoint > myControlArray[n]){
                                 myControlArray[n] = (myEndPoint - myStartPoint) + 4;
                                 }
                             }
                        }
                    }
                myTabStops = new Array();
                for(i=0;i<myTabLength;i++){
                    if(i == 0){
                        myTabStops[i] = myControlArray[i];
                        }
                    else{
                        myTabStops[i] = myControlArray[i] + myTabStops[i-1];
                        }
                    }
                myStory.tabStops.everyItem().remove();
                for(i=0;i<myTabLength;i++){
                      myStory.tabStops.add({position:myTabStops[i]});
                     }
                 }
                catch(e){
                    }
                function myReset()
                            {
                            app.findTextPreferences = NothingEnum.nothing;
                            app.changeTextPreferences = NothingEnum.nothing;
                            }
                

                 

                Let me know how it goes for you.

                 

                It works on a text frame or insertion point selection.

                 

                Roy