1 Reply Latest reply on May 20, 2014 11:15 PM by Jump_Over

    Correct my script

    hasvi Level 1

      Hi,

       

      I have created base align script for spread page base alignment. Please correct my script:

       

      My script concept is first it will done leading adjustment, if it is not enough next it will start above space adjustments in Heading levels(H1, H2, H3).

       

      My space betweent bottom margin and last line base of textframe is not equal to same,

      For instance myBaseLineDiff is 14. My para leading is 12 means first it will try to give tracking value (so 14-12=2), for the remaining space(2) it will adjust the above space of heading levels.

       

      this is my script, its running but there is no changes, please help me:

      var myDoc=app.activeDocument;
      var myPages=myDoc.pages;
      var globPage;
      var globTextFrame;
      var globPara;
      var globBaseLineDiff;
      var arrHeadStyles=new Array();
      
      
      ProcessEachPage();
      
      
      function ProcessEachPage()
      {
          for(var i=0;i<myPages.length;i++)
          {
              globPage=myPages[i];
              arrHeadStyles=new Array();
              var myTextFrames=globPage.textFrames;
              for(var k=0;k<myTextFrames.length;k++)
              {
                  globTextFrame=myTextFrames[k];
                  if(globTextFrame.label=="SCRIPT_TEXTFRAME")
                  {
                      globBaseLineDiff=CheckBaseAlign();
                      if(globBaseLineDiff!=0)
                      {
                             GetHeadStyles();
                             ProcessTracking(10); 
                             if(arrHeadStyles.length!=0)
                             {
                             adjustHeadingSpace(30);
                             //ProcessTracking(10); 
                             //adjustHeadingSpace(6);
                             //adjustHeadingSpace(10);
                              }
                        }
                      }
                  }
              }
      }
      
      
      function CheckBaseAlign()
      {
              var myPageMargin = globPage.marginPreferences;
              var myPageBounds=globPage.bounds;
              var myPageBaseLine=myPageBounds[2] -myPageMargin.bottom;
              
              if(!globTextFrame.lines[-1].isValid)
              {
                  return 0;
                  }
              
              var myTFBaseLine=globTextFrame.lines[-1].baseline;
              var myBaseLineDiff=parseFloat(myPageBaseLine) - parseFloat(myTFBaseLine);
              
               if(myBaseLineDiff > 0.1 && myBaseLineDiff  < - 0.1 )
               {
                   return 0;
                   }
               else
               {
                   return myBaseLineDiff ;
                   }
      }
      
      
      function adjustHeadingSpace(myInputSpace)
      {
                  for(var k=0;k<arrHeadStyles.length;k++)
                  {
                      applyStyleSpace(arrHeadStyles[k]);
                  }
                  globBaseLineDiff=CheckBaseAlign();
              
                  var myAdjMode;
                  var myAdjSpace;
                  //alert(myInputSpace * arrHeadStyles.length+"\n"+globBaseLineDiff );
                  if((myInputSpace * arrHeadStyles.length) > globBaseLineDiff )
                  {
                      myAdjMode=globBaseLineDiff  % arrHeadStyles.length;
                      myAdjSpace=Math.floor (globBaseLineDiff / arrHeadStyles.length) ;
                      }
                  else
                  {
                      myAdjMode=(myInputSpace * arrHeadStyles.length)  % arrHeadStyles.length;
                      myAdjSpace=Math.floor ((myInputSpace * arrHeadStyles.length) / arrHeadStyles.length) ;
                      }
                  
                  if(globBaseLineDiff > arrHeadStyles.length)
                  {
                              for(var k=0;k<arrHeadStyles.length;k++)
                              {
                                  arrHeadStyles[k].spaceBefore=arrHeadStyles[k].spaceBefore+myAdjSpace;
                                  }
                  }
                  
                  for(var k=0;k<arrHeadStyles.length;k++)
                   {
                                  
                                  if(myAdjMode < 1)
                                  {
                                      arrHeadStyles[k].spaceBefore=arrHeadStyles[k].spaceBefore+myAdjMode;
                                      break;
                                   }
                                  else
                                  {
                                      arrHeadStyles[k].spaceBefore=arrHeadStyles[k].spaceBefore+1;
                                      myAdjMode =myAdjMode - 1;
                                  }
                   }
      }
      
      
      function ProcessTracking(myTrackSpace)
      {
              var myParas=globTextFrame.paragraphs;
               globBaseLineDiff=CheckBaseAlign();
              for(var i=0;i<myParas.length;i++)
              {
                      var myParaLineLen=myParas[i].lines.length;
                      var myParaNewLineLen;
                      myParas[i].tracking=0;
                      if(myParas[i].leading <= globBaseLineDiff)
                      {
                          for(var k=1;k<=myTrackSpace;k++)
                          {
                                  myParas[i].tracking=k;
                                  myParaNewLineLen=myParas[i].lines.length;
                                  if(myParaLineLen !=myParaNewLineLen)
                                  {
                                      break;
                                      }
                              }
                              if(myParaLineLen ==myParaNewLineLen)
                          {
                              myParas[i].tracking=0;
                              }
                      }
              }
          
      }
      
      
      function GetHeadStyles()
      {
          for(var i=0;i<globTextFrame.paragraphs.length;i++)
          {
              if(globTextFrame.paragraphs[i].appliedParagraphStyle.name.match(/^H([1-9]+)$/) &&  i != 0)
              {
                  arrHeadStyles.push(globTextFrame.paragraphs[i]);
                  }
              }
      }
      
      
      
      
      function applyStyleSpace(myHeadPara)
      {
          myHeadPara.spaceBefore=myHeadPara.appliedParagraphStyle.spaceBefore;
      }
      

       

       

      by

      Hasvi

        • 1. Re: Correct my script
          Jump_Over Level 5

          Hi,

           

          Look into your CheckBaseAlign function()

           

          If statement:

               if(myBaseLineDiff > 0.1 && myBaseLineDiff  < - 0.1 )

          is always false. Is it your goal?

          Or you mentioned a negation like:

               if ( !(myBaseLineDiff > 0.1 || myBaseLineDiff  < - 0.1) )

           

          Look into your adjustHeadingSpace(myInputSpace)

          If myInputSpace is an integer ==> line like this

               myAdjMode=(myInputSpace * arrHeadStyles.length)  % arrHeadStyles.length;

          always returns 0. Is it you expected here?

               myAdjSpace=Math.floor ((myInputSpace * arrHeadStyles.length) / arrHeadStyles.length) ;

          always returns myInputSpace. Is it you expected here?

           

          It works on my side with simple examples.

           

          Did you try to debug your code using breapoints (checking variables values while execution is paused)

          with your doc?

           

          Jarek