12 Replies Latest reply on May 22, 2014 6:37 AM by Laubender

    Find Tab position

    BEGINNER_X Level 3

      Hi All,

       

      I need to find the tab position and alignment, and change the following:

       

      1. Change the first tab in the paragraph (Left align tab into Center align tab)

       

      2. Change the second tab in the paragraph (Left align tab into Right align tab)

       

      Before script:

      Screen Shot 2014-05-21 at 1.38.48 PM.png

       

      After Script:

       

      Screen Shot 2014-05-21 at 1.40.26 PM.png

        • 1. Re: Find Tab position
          Chinnadk Level 4

          Hi,

           

          Try this,

           

          var paras = app.activeDocument.stories.everyItem().paragraphs.everyItem().getElements();

          for(var i=0;i<paras.length;i++)

          {

              var tabstops = paras[i].tabStops;

              if(tabstops.length>1){

                  tabstops[0].alignment = TabStopAlignment.CENTER_ALIGN;

                  tabstops[1].alignment = TabStopAlignment.RIGHT_ALIGN;

              }

          }

           

          Regards,

          Chinna

          • 2. Re: Find Tab position
            BEGINNER_X Level 3

            Hi Chinna,

             

            Thank You for your immediate help. And it works like a charm.... And also one more help..

             

            Is it possible to align the tabs for fixing the equal space between the entries???

             

            [when we adjust the tabstop alignment (i.e., left to center there is possible for the inconsistency space)

             

             

            Thanks

            Siraj

            • 3. Re: Find Tab position
              Chinnadk Level 4

              Hi Siraj,

               

              Try this.

               

              var paras = app.activeDocument.stories.everyItem().paragraphs.everyItem().getElements();

              var a = 0;

              for(var i=0;i<paras.length;i++)

              {

                  var tabstops = paras[i].tabStops;

                  for(var j =0;j<tabstops.length;j++)

                  {

                          tabstops[j].position =  a+=20;

                      }

              }

               

              Regards,

              Chinna

              • 4. Re: Find Tab position
                BEGINNER_X Level 3

                Hi Chinnadk,

                 

                Excellent Stuff!!!!!

                 

                But there is a small confusion, what I explain in the above thread.

                 

                Please refer the below screenshots:

                Screen Shot 2014-05-22 at 10.20.56 AM.png

                After using the below script:

                var paras = app.activeDocument.stories.everyItem().paragraphs.everyItem().getElements();

                for(var i=0;i<paras.length;i++)

                {

                    var tabstops = paras[i].tabStops;

                    if(tabstops.length>1){

                        tabstops[0].alignment = TabStopAlignment.CENTER_ALIGN;

                        tabstops[1].alignment = TabStopAlignment.RIGHT_ALIGN;

                    }

                }

                 

                Output looks likes:

                Screen Shot 2014-05-22 at 10.22.26 AM.png

                 

                But the Final Requirement is,

                1. Extend upto the end of textframe

                2. Equal space between the three entry

                 

                I need a script for the following things only

                Screen Shot 2014-05-22 at 10.24.00 AM.png

                Once again Great thankful, If you help for this request.

                 

                Could anyone help for this complex task?

                 

                Thanks for All

                Siraj

                • 5. Re: Find Tab position
                  Harbs. Level 6

                  You need to find the tab characters and check the horizontalOffset and endHorizontalOffset. Based on those values, you know the width of the text sections and you can position the middle tab to fit. The last tab should be a right aligned tab at the very end of the text frame.

                  • 6. Re: Find Tab position
                    Chinnadk Level 4

                    Hi Siraj,

                     

                    Try this.

                     

                    var paras = app.activeDocument.stories.everyItem().paragraphs.everyItem().getElements();

                    for(var i=0;i<paras.length;i++)

                    {

                        var gb = paras[i].parentTextFrames[0].geometricBounds

                        var width = gb[3] - gb[1];

                        var tabstops = paras[i].tabStops;

                        if(tabstops.length>1){

                            tabstops[0].alignment = TabStopAlignment.CENTER_ALIGN;

                            tabstops[0].position = width/2;

                            tabstops[1].alignment = TabStopAlignment.RIGHT_ALIGN

                            tabstops[1].position = width;

                        }

                    }

                     

                    Regards,

                    Chinna

                    • 7. Re: Find Tab position
                      Harbs. Level 6

                      You are missing the requirement to make the space equal.

                       

                      Instead of tabstops[0].position = width/2;, you need to compute where the middle between the first text span and the last text span would be using horizontalOffset and endHorizontalOffset like I mention above...

                      • 8. Re: Find Tab position
                        BEGINNER_X Level 3

                        Hi Chinna,

                         

                        Thank you for your beautiful coding...

                         

                        As Harbs said, there is inconsistency space between the entries.

                         

                        Just now, I learn the horizontal offset concept. Please help.

                         

                        Screen Shot 2014-05-22 at 12.44.27 PM.png

                        • 9. Re: Find Tab position
                          BEGINNER_X Level 3

                          Hi Chinna/Harbs,

                           

                          Trying the below code, but not suceed.

                           

                          var mySel1 = app.selection[0].lines[0].words[0].characters[-1].insertionPoints[0].endHorizontalOffset;

                           

                          var myFinal1 = Number(mySel1)

                           

                          var mySel2 = app.selection[0].lines[0].words[2].characters[0].insertionPoints[0].endHorizontalOffset;

                           

                          var myFinal2 = Number(mySel2)

                           

                          var myFinal3 = Nuumber(myFinal2 - myFinal1);

                           

                          alert("myFinal3: " + myFinal3)

                           

                          app.selection[0].lines[0].tabStops[0].alignment = TabStopAlignment.CENTER_ALIGN;

                           

                          app.selection[0].lines[0].tabStops[0].position = myFinal3/2;             //Here is the problem

                           

                           

                          Thanks

                          Siraj

                          • 10. Re: Re: Find Tab position
                            Harbs. Level 6

                            There's a few ways to get the values. If there's no spaces, you can do something like this:

                            var para = app.selection[0].paragraphs[0];
                            var firstWidth = para.words[0].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset;
                            var lastWidth = para.words[-1].insertionPoints[-1].horizontalOffset - para.words[-1].insertionPoints[0].horizontalOffset;
                            var widthDiff = lastWidth - firstWidth;
                            tabstops[0].position = (width/2) - (widthDiff/2);
                            

                             

                            Otherwise, you need to find the tab characters and do some calculations based on that.

                            • 11. Re: Find Tab position
                              BEGINNER_X Level 3

                              Hi Harbs/Chinna,

                               

                              Thank you for spending your valuable time for me.

                               

                              Help of your code, I am modifying my script as per requirement.

                               

                              Expect the same support in future too.

                               

                              You are inspiring lot of junior or beginner level of programmers like me.

                               

                              Again thanks a lot!!!!

                               

                              Thanks

                              Siraj

                              • 12. Re: Find Tab position
                                Laubender Adobe Community Professional & MVP

                                @Harbs – yes.

                                 

                                @Siraj:

                                But using horizontalOffset will only work on not rotated text frames or not rotated texts inside text frames (or table cells), because horizontalOffset is tied to the coordinate system of the spread or the page.

                                 

                                Of course we could check for that before doing any calculations.

                                Eg. by taking the meassurements of the values for baseline (and adding baselineShift) of two distant characters in the same line of text and compare them.

                                 

                                Oh. And what about extremely kerned or tracked characters? Knit-picking, I know… ;-)


                                And if you want to compare positions with horizontalOffset to other page items it's always recommended setting some of the ViewPreferences like rulerOrigin (RulerOrigin.SPREAD_ORIGIN), verticalMeasurementUnits (MeasurementUnits.POINTS) and horizontalMeasurementUnits(MeasurementUnits.POINTS).

                                 

                                Uwe