4 Replies Latest reply on Mar 6, 2017 12:16 PM by Obi-wan Kenobi

    Code to be evaluated! [016] Tabs Position! …

    Obi-wan Kenobi Adobe Community Professional

      Hi All,

       

      The deal is simple:

      You select text with tabs and run the script below! You just need to enter the new font size!

       

      Capture d’écran 2017-03-06 à 02.56.19.png

       

      The script seems to work! The position of the tabs is recalculated, depending on the new font size, here 16 pts => 8 pts!

       

      Capture d’écran 2017-03-06 à 02.59.52.png

       

      … If I want to go back (no undo!): 8 pts => 16 pts and run the script again:

       

      Capture d’écran 2017-03-06 à 03.00.13.png

       

      No idea to correct this!  Thanks for help! 

       

       

      var w = new Window ('dialog {alignChildren: "top"}', 'New Tabs position! …');
       w.alignChildren = "right";
      
      
          var group1 = w.add ('group');
              group1.add ('statictext {text: "New Font Size:"}');
      
      
          var UIy = group1.add ("edittext", undefined, 0);
              UIy.characters = 5;
              UIy.minimumSize.width = 60;
              UIy.maximumSize.width = 60;
              UIy.active = true;
      
          var buttons = w.add ('group {alignment: "center"}');
              buttons.add ('button {text: "OK"}');
              buttons.add ('button {text: "Cancel"}');
      
      
          var group2 = w.add ('group');
              group2.add ('statictext {text: "(by Michel Allio, March 2017)"}');
      
      
      if ( w.show () == 1 )  var y = Number (UIy.text);
      else  exit();
      
      
      app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "New Tabs position! …");
      
      
      function main()     
          {
              var mySel = app.selection[0],
              myStops = mySel.tabStops,
              S = myStops.length,
              x = mySel.pointSize;
              mySel.pointSize = y;
              for ( var s = 0 ; s < S; s++ )  myStops[s].position = myStops[s].position/x*y;
          }
      

       

       

      (^/)

        • 1. Re: Code to be evaluated! [016] Tabs Position! …
          Jump_Over Level 5

          Hi,

           

          For me it looks like direction of loop matter.

          If original pointSize is higher than UI value ==> step++

          Else ==> step --

           

          /...
                  if (mySel.pointSize > y)
                      for ( var s = 0 ; s < S; s++ ) 
                          myStops[s].position = myStops[s].position/x*y; 
                else
                      for ( var s = S-1 ; s >= 0; s-- ) 
                          myStops[s].position = myStops[s].position/x*y; 
          //...
          

           

          Thats to avoid interaction inbetween currently modified tabStop and the next ones (which possibly would change a position [index])

          Jarek

          • 2. Re: Code to be evaluated! [016] Tabs Position! …
            Liphou Level 1

            Hi,

            The problem seems to be when a position exceeds or comes before another tab order is no longer good, right?

             

            Bonjour

             

            Le problème semble être quand une position dépasse ou viens avant un autre tabulation l'ordre n'est plus bon, non?

             

            Philippe

            • 3. Re: Code to be evaluated! [016] Tabs Position! …
              Liphou Level 1

              Je viens de faire des tests là ça fonction mais probablement incomplet dans les propriété :

               

              /...
              var positions=[];
                 for ( var s = 0 ; s < S; s++ )  { 
                     
                          positions.push([myStops[s].alignment, myStops[s].position] );
                  }
                  myStops.everyItem().remove();
                   for ( var s = 0 ; s < S; s++ )  { 
                         mySel.tabStops.add({alignment:positions[s][0], leader:"", position:(positions[s][1]/x*y)})
                   }
              .../
              
              • 4. Re: Code to be evaluated! [016] Tabs Position! …
                Obi-wan Kenobi Adobe Community Professional

                Thanks Jarek and Philippe!

                 

                I think it works fine now as:

                 

                function main()     
                    {
                        var mySel = app.selection[0],
                        myStops = mySel.tabStops,
                        S = myStops.length,
                        x = mySel.pointSize;
                        mySel.pointSize = y,
                        myStops_0 = [];  
                        for ( var s = 0 ; s < S; s++ )  myStops_0.push([myStops[s].alignment, myStops[s].leader, myStops[s].position]);  
                        myStops.everyItem().remove();  
                        for ( var s = 0 ; s < S; s++ )  myStops.add({alignment: myStops_0[s][0], leader: myStops_0[s][1], position: myStops_0[s][2]/x*y});
                    }
                

                 

                 

                (^/)