13 Replies Latest reply on Aug 10, 2016 8:23 AM by tpk1982

    Table tab alignment

    tpk1982 Level 4

      Hi,

       

      I am with peculiar script requirement. I need to fix the table.

       

      Conditions:

       

      1) First column align left

       

      2) Second column align 0.6" from left

       

      3) Last column leave as it is

       

      4)  The remaining Columns center aligns in between the maximum length of last and 2nd column with even space distribution between

       

      Screengrab:

       

      Screen Shot 2016-08-09 at 6.07.22 PM.png

       

      Tried coding:

       

      function main(){
      var para = app.selection[0];  
      mySelWords = para.texts.everyItem().words.length;
      if (para.hasOwnProperty ("paragraphs") == false) return;
      var paras = para.paragraphs.everyItem().getElements();
      myarray=[];
      var s=0;
      for(i=2;i<mySelWords;i++){
      var firstWidth1 = para.words[i].insertionPoints[-1].horizontalOffset - para.words[i].insertionPoints[0].horizontalOffset; 
      myarray.push(firstWidth1);
      s=s+Number(firstWidth1);
      }
      var mytabspaces=mySelWords-3;
      var line = para.lines[0];
      var totalwordlength = line.characters[-1].horizontalOffset- line.characters[0].horizontalOffset;
      var lastword = para.words[mySelWords-1].insertionPoints[-1].horizontalOffset - para.words[mySelWords-1].insertionPoints[0].horizontalOffset; 
      var firsttwowordswidth= para.words[1].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset;
      var therequiredspace= (totalwordlength-(firsttwowordswidth+lastword))
      var toaddspace=((therequiredspace-s)/mytabspaces)
      var thirdword = ((para.words[2].insertionPoints[-1].horizontalOffset - para.words[2].insertionPoints[0].horizontalOffset)/2); 
      var fourthword = ((para.words[3].insertionPoints[-1].horizontalOffset - para.words[3].insertionPoints[0].horizontalOffset)/2); 
      var fifthword = ((para.words[4].insertionPoints[-1].horizontalOffset - para.words[4].insertionPoints[0].horizontalOffset)/2); 
      var sixthword = ((para.words[5].insertionPoints[-1].horizontalOffset - para.words[5].insertionPoints[0].horizontalOffset)/2); 
      
      
      for (n = 0; n < paras.length; n++) {
        paras[n].tabStops.everyItem().remove();
        }
      
      
      for (n = 0; n < paras.length; n++) {
              paras[n].tabStops.add( {alignment: TabStopAlignment.LEFT_ALIGN, position: "0.6 in"});
      
      
              var secondtab = ((para.words[1].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset)); 
              paras[n].tabStops.add( {alignment: TabStopAlignment.CENTER_ALIGN, position: secondtab+toaddspace+thirdword});
              
              var thirdtab = ((para.words[2].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset));
              paras[n].tabStops.add( {alignment: TabStopAlignment.CENTER_ALIGN, position: thirdtab+toaddspace+fourthword});
              
              var fourthtab = ((para.words[3].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset)); 
              paras[n].tabStops.add( {alignment: TabStopAlignment.CENTER_ALIGN, position: fourthtab+toaddspace+fifthword});
              
              var fifthtab = ((para.words[4].insertionPoints[-1].horizontalOffset - para.words[0].insertionPoints[0].horizontalOffset));
              paras[n].tabStops.add( {alignment: TabStopAlignment.CENTER_ALIGN, position: fifthtab+toaddspace+sixthword});
        
              paras[n].tabStops.add( {alignment: TabStopAlignment.RIGHT_ALIGN, position: totalwordlength-"0.0243 in"});
      
      
       }
          }
      }
      
      
      if (app.selection.length > 0) main();
      

       

       

      Problem:

       

      The above coding not worked because second column changed from Product  to Product Dim. So counting words will be mistaken by doing with my method.

       

      Please give some suggestion how to do for the requirement.

       

      Regards,

      K

        • 1. Re: Table tab alignment
          scriptworker Level 1

          Following your code, you should do something like this I think:

           

          var w1offset = para.words[1].insertionPoints[-1].horizontalOffset;

          var w2offset = para.words[2].insertionPoints[-1].horizontalOffset;

           

          var secondtab = (((w1offset + w2offset) - para.words[0].insertionPoints[0].horizontalOffset));

           

          Michel

          • 2. Re: Table tab alignment
            tpk1982 Level 4

            Thanks Michel.

             

            But it is not working I hope i need to get character with tab value using index

            • 3. Re: Table tab alignment
              karthiks94217840 Level 2

              Hi TPK,

               

              @Your Question: i need to get character with tab value using index.

               

              In the below script for distribute tabbed longest word(character) equally. I don't now tpk this right way to reach your work. Just some idea only.

               

              ***************************

               

              var gap = 6.5;

                          if (app.selection.length > 0) main();

                          function main() {

                              var sel = app.selection[0];

                              if (sel.hasOwnProperty ("paragraphs") == false) return;

                              var paras = sel.paragraphs.everyItem().getElements();

                              if (paras.length == 0) return;

                              for (n = 0; n < paras.length; n++) {

                                  paras[n].tabStops.everyItem().remove();

              //~                     alert(paras[n].contents);  //Remove tab preview points

                              }

                              var tabs = new Array();

                              app.findGrepPreferences = NothingEnum.NOTHING;

                              app.findGrepPreferences.findWhat = "\t";

                              for (n = 0; n < paras.length; n++) {

                                  tabs.push( paras[n].findGrep() );

                              }

                              var x = 0;

                              var t = 0;

                              var n;

                              var ptabs;

                              var got_all;

                              var offset = paras[0].insertionPoints.firstItem().horizontalOffset;

                              do {

                                  got_all = true;

                                  for (n = 0; n < paras.length; n++) {

                                      ptabs = tabs[n];

              //~                         alert(ptabs.length);

                                      if ( ptabs.length > t ) {

                                          x = Math.max( x, ptabs[t].horizontalOffset );

                                          got_all = false;

                                      }

                                  }

                                  for (n = 0; n < paras.length; n++) {

                                      paras[n].tabStops.add( {alignment: TabStopAlignment.LEFT_ALIGN, position: x-offset+gap});

                                  }

                                  t++;

                              } while ( !got_all );

                          }

              *************************

               

              Best Wishes

              KS

              • 4. Re: Table tab alignment
                tpk1982 Level 4

                Thanks KS.. i have the above script from forum.. but my requirement is quite different

                • 5. Re: Table tab alignment
                  scriptworker Level 1

                  Thanks Michel.

                   

                  But it is not working  I hope i need to get character with tab value using index

                   

                  This was just a hint.

                  Did you adapt the rest of your code?

                   

                  Michel

                  • 6. Re: Table tab alignment
                    tpk1982 Level 4

                    Hi Michel,

                     

                    It will be useful if we know the words between tab are one/two/three. But if i dont know the number of words in between tab then only problem starts.

                    • 7. Re: Table tab alignment
                      Laubender Adobe Community Professional & MVP

                      Hi together,

                      is your task to present the "table" visually in a way that is pleasing to the eye?


                      Then, you have to find an algorithm that can calculate white space (as space in two dimensions, not as distance between insertion points) and bring that in correlation with heuristics you can only derive from "table" samples that are indeed pleasing to the eye.

                       

                      Regards,

                      Uwe

                      • 8. Re: Table tab alignment
                        tpk1982 Level 4

                        Hi Uwe,

                         

                        Yes in my script i find the white space and divided by the number os tabs to be fixed. So with the value i set. Insertion point is useful to find the white spaces.

                         

                        Thanks,

                        Karthi

                        • 9. Re: Table tab alignment
                          Laubender Adobe Community Professional & MVP

                          Sorry, but I think, your algorithm is not good enough.
                          I cannot see, that you are calculating white space in two dimensions.

                           

                          Just distances.

                          Not space.


                          And I think, that's not enough.
                          You'll need the area of the white space left and right from a tab from the whole column according to the single text frame the text is sitting in. But that would be only a start. Important as well is the distribution, the shape, that this white area is forming to react on it.

                           

                          Regards,
                          Uwe

                          1 person found this helpful
                          • 10. Re: Table tab alignment
                            tpk1982 Level 4

                            Thanks Uwe. I am afraid i understood your words correctly.

                             

                            Is it you mean i need to use geometric bound concept to find white space in two dimension? If no could you please give suggestion how to find?

                             

                            Regards,

                            Karthi

                            • 11. Re: Table tab alignment
                              Laubender Adobe Community Professional & MVP

                              tpk1982 wrote:

                              Thanks Uwe. I am afraid i understood your words correctly.

                              Um, yes.

                              You have to compare shapes. And you have to find an algorithm that can evaluate two shapes.
                              Furthermore you are not free to change the width of the shapes by moving tabs around, because you are limited to a specific line width in total.

                               

                              That's no easy task.
                              And I assume, that a script, that can do this with reasonable results, would be very, very slow.

                               

                              Maybe it's better to write a script with an UI, that could assist a human being, the InDesign user, to find the "best" compromise? A UI that is easier to handle than the current one in InDesign for manipulating tabs. A UI, that is not working with selected text, but with a selected text frame where the white space is still showing as white space …

                               

                              Regards,
                              Uwe

                              • 12. Re: Table tab alignment
                                tpk1982 Level 4

                                Hi Uwe,

                                 

                                Thank you for your guidance. So i need to change the concept and start a new script with UI. 

                                 

                                Is it any sample that you have tab alignment using UI? If no then no issues. Sorry to ask this.

                                 

                                Thanks,

                                Karthi

                                • 13. Re: Table tab alignment
                                  tpk1982 Level 4

                                  Hi All,

                                   

                                  Any other suggestions?

                                   

                                  Regards,

                                  Karthi