6 Replies Latest reply on Nov 17, 2016 2:34 PM by Obi-wan Kenobi

    Code to be evaluated! [008] // Invert Para rule Indents! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters,

       

      This script inverts manual and para styles para rules indents (the para style(s) is(are) updated!).

       

      It seems to work but I find its writing … very ugly! No idea to make it more beautiful!

      The first loop "for" inverts all the indents. The second one updates the para styles, according the "if" statement.

       

       

      app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Invert Para Rule Indents! …");  
        
      function main()  
      {  
          var myDoc = app.activeDocument;  
          var myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements();
          
          for (P = 0; P < myParas.length; P++)    
          {    
              var AL = myParas[P].ruleAboveLeftIndent;    
              var AR = myParas[P].ruleAboveRightIndent;  
              var BL = myParas[P].ruleBelowLeftIndent;    
              var BR = myParas[P].ruleBelowRightIndent;  
              myParas[P].ruleAboveLeftIndent = AR;  
              myParas[P].ruleAboveRightIndent = AL;  
              myParas[P].ruleBelowLeftIndent = BR;  
              myParas[P].ruleBelowRightIndent = BL;  
          } 
      
      
          for (P = 0; P < myParas.length; P++)    
          {    
              var AL = myParas[P].ruleAboveLeftIndent;
              var AR = myParas[P].ruleAboveRightIndent;  
              var BL = myParas[P].ruleBelowLeftIndent;    
              var BR = myParas[P].ruleBelowRightIndent;  
              
              var myStyle = myParas[P].appliedParagraphStyle;
      
      
              var ALs = myStyle.ruleAboveLeftIndent;
              var ARs = myStyle.ruleAboveRightIndent;  
              var BLs = myStyle.ruleBelowLeftIndent;    
              var BRs = myStyle.ruleBelowRightIndent;
              
              if ( AL == ARs && AR == ALs && BL == BRs && BR == BLs ) {
                      myStyle.ruleAboveLeftIndent = ARs;    
                      myStyle.ruleAboveRightIndent = ALs;    
                      myStyle.ruleBelowLeftIndent = BRs;    
                      myStyle.ruleBelowRightIndent = BLs;    
              }
          }
      
      
      }
      

       

       

      Thanks in advance! 

       

      (^/)

        • 1. Re: Code to be evaluated! [008] // Invert Para rule Indents! …
          Jump_Over Level 5

          Hi,

           

          Let me ask what exactly is a goal?

          According to your code is:

          1. inverse left/right indent of both rules of every paragraph found in a doc

          2. redefine applied paraStyle but ONLY if this style IS NOT manually overriden

           

          Is this correct?

           

          Jarek

          • 3. Re: Code to be evaluated! [008] // Invert Para rule Indents! …
            Jump_Over Level 5

            So at point 2 there is a conflict:

            -- one style is possibly applied to many paragraphs

            -- some of them could be manually modified - some not.

            -- script will sometimes redefine a style definition - sometimes will not.

             

            I suggest to reconsider this "if" statement.

             

            Jarek

            • 4. Re: Code to be evaluated! [008] // Invert Para rule Indents! …
              Obi-wan Kenobi Adobe Community Professional

              I've played with this situation:

              4 paras with para rules [of course, they run per 2, above & below].

              In "black color", manual para rules ; the para style has no para rules by default.

              In "red color", a para with para rules option. the first para is pure (see selection), the second has its para rules option manually modified.

               

              Before the script:

               

              Capture d’écran 2016-11-17 à 21.13.52.png

               

              After:

               

              Capture d’écran 2016-11-17 à 21.14.12.png

               

              All the indents are inverted.

              As you see, the para style applied to the first "red" para is always pure (updated).

              The three other are always overridden!

               

              So, the script seems to run well!

               

              (^/)

              • 5. Re: Code to be evaluated! [008] // Invert Para rule Indents! …
                Jump_Over Level 5

                OK. It goes from the back side - first para after style... got your point.

                 

                I built it this way:

                app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Invert Para Rule Indents! …");   
                function main()   
                {   
                    var
                        myDoc = app.activeDocument, 
                        myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements(),
                        cPara; 
                    // to clear last run stored labels
                    myDoc.paragraphStyles.everyItem().label= "";
                    //
                    while (cPara = myParas.shift())
                        if (!testStyle(cPara))
                            redefinePara(cPara);   
                    }
                function testStyle(thisPara) {
                    // to detect the way of redefining ==> style OR para
                    if (!thisPara.appliedParagraphStyle.index ||    //    none
                        thisPara.appliedParagraphStyle.ruleAboveLeftIndent != thisPara.ruleAboveLeftIndent||
                        thisPara.appliedParagraphStyle.ruleAboveRightIndent != thisPara.ruleAboveRightIndent||
                        thisPara.appliedParagraphStyle.ruleBelowLeftIndent != thisPara.ruleBelowLeftIndent||
                        thisPara.appliedParagraphStyle.ruleBelowRightIndent != thisPara.ruleBelowRightIndent
                        ) return false;
                    // to avoid processing a style which is redefined already
                    if (thisPara.appliedParagraphStyle.label != "inverted") redefineStyle (thisPara);
                    //
                    return true;
                    }
                function redefineStyle (thisPara) {
                    // no rules no action
                    if (!(thisPara.ruleAbove && thisPara.ruleBelow) ) return;
                    //
                    var frozenLeftVal = thisPara.ruleAboveLeftIndent;
                    //
                    thisPara.appliedParagraphStyle.ruleAboveLeftIndent = thisPara.ruleAboveRightIndent;
                    thisPara.appliedParagraphStyle.ruleAboveRightIndent = frozenLeftVal;
                    //
                    frozenLeftVal = thisPara.ruleBelowLeftIndent;
                    thisPara.appliedParagraphStyle.ruleBelowLeftIndent = thisPara.ruleBelowRightIndent;
                    thisPara.appliedParagraphStyle.ruleBelowRightIndent = frozenLeftVal;
                    // to avoid processing style redefined already
                    thisPara.appliedParagraphStyle.label = "inverted";
                    }
                function redefinePara (thisPara) {
                    // no rules no action
                    if (!(thisPara.ruleAbove && thisPara.ruleBelow) ) return;
                    //
                    var frozenLeftVal = thisPara.ruleAboveLeftIndent;
                    //
                    thisPara.ruleAboveLeftIndent = thisPara.ruleAboveRightIndent;
                    thisPara.ruleAboveRightIndent = frozenLeftVal;
                    //
                    frozenLeftVal = thisPara.ruleBelowLeftIndent;
                    thisPara.ruleBelowLeftIndent = thisPara.ruleBelowRightIndent;
                    thisPara.ruleBelowRightIndent = frozenLeftVal;
                    }
                

                 

                why this way:

                1. to collect part of code in functions

                2. to avoid redefining style (updating para) which is already redefined

                3. to skip paras with no rules

                ...

                4. I suggest to use "ENTIRE_SCRIPT" mode instead of "FAST_ENTIRE_SCRIPT". I've experienced many times - while using "Fast mode" - Indesign doesnt update changes made to object (text.contents, box.resize, etc) by script.

                 

                Jarek

                PS to tell you the true - not fully tested...

                • 6. Re: Code to be evaluated! [008] // Invert Para rule Indents! …
                  Obi-wan Kenobi Adobe Community Professional

                  Aha!  Thanks Jarek!

                  I'll test it tonight and I"ll give you my comments tomorrow!

                   

                  Good night!

                   

                  (^/)