9 Replies Latest reply on Dec 4, 2016 9:29 AM by Obi-wan Kenobi

    Problem with an "if" statement! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters,

       

      I'm interested by a space and if the char style applied to it!

      So, I've written this line:

       

      if ( myLast.contents == " " && myLast.appliedCharacterStyle = "Red-Underlined" ) ……………… 
      

       

      I'm wrong!!    Apparently, the 2nd part is not correct: I don't want to apply the char style! I only want to exclude the bad spaces and don't treat them when the char style is not applied to them!!!    So, I would like: "space" + "char style applied"!

       

      Thanks in advance!

       

      (^/)

        • 1. Re: Problem with an "if" statement! …
          Peter Kahrel Adobe Community Professional & MVP

          if ( myLast.contents == " " && myLast.appliedCharacterStyle.name != "Red-Underlined" )

          • 2. Re: Problem with an "if" statement! …
            Obi-wan Kenobi Adobe Community Professional

            Thanks Peter!

             

            It seems to be better but something doesn't work correctly!!!

             

            More explanations:

             

            The deal is to add another answer to this topic:  Avoid underline at the end of a line

             

            Jongware gives us an old and great solution that works well but I find it too brutal without taking care of future modifications.

             

            I've a different approach:

             

            1/ I treat the "underlinings" with a grep style! … and the "no-underlined" too!! [screenshot 1]

            To apply the underlining on text part, I insert 2 non-joiners before and 2 after the text.

            As you see in the screenshot 2, bad underlined spaces at the line ends.

             

            2/ I've inserted 3 direct char styles for sample. [screenshot 2]

            I launch the Script for the first time. Result: screenshot 3.

             

            3/ In a second time, I've inserted text! So the layout needs to be updated!

            I launch the script another time!  [screenshot 4]

             

            The script seems to correctly update! … But not! If I launch the script several times, it inserts a bad result at the line n-1 adding non-joiners at the line end!!!!

             

            Boring and no idea to correct this! 

             

            Capture d’écran 2016-12-02 à 19.05.47.png

             

            Capture d’écran 2016-12-02 à 18.55.18.png

             

            Capture d’écran 2016-12-02 à 18.55.41.png

             

            Capture d’écran 2016-12-02 à 18.56.53.png

             

            The code:

             

            app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");  
              
            function main ()        
            {  
            
            
            var myDoc = app.activeDocument;
            
            
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.appliedCharacterStyle = "Red-Underlined";
            app.findGrepPreferences.findWhat = "~j(?!~j)|(?<!~j)~j";
            app.changeGrepPreferences.changeTo = "";
            myDoc.changeGrep();
            
            
            app.findGrepPreferences = app.changeGrepPreferences = null;  
            app.findGrepPreferences.appliedCharacterStyle = "Red-Underlined";
            myFound = myDoc.findGrep();
            
            
            for (F = 0; F < myFound.length; F++)  
                {  
                    myF = myFound[F];
                
                    for (L = 0; L < myF.lines.length; L++)  
                        {  
                            var myLast = myF.lines[L].characters.item(-1);
            
            
                            if ( myLast.contents == " " && myLast.appliedCharacterStyle.name != "Red-Underlined" )  myLast.insertionPoints[0].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER;  
                        }
                }
            
            
            }
            
            

             

             

            (^/)

            • 3. Re: Problem with an "if" statement! …
              Obi-wan Kenobi Adobe Community Professional

              Gotcha!

               

              This code works:

               

              app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");  
                
              function main ()        
              {  
              
              
              var
              myDoc = app.activeDocument,
              myCstyle = "Red-Underlined",               //  CHARACTER STYLE TO BE DEFINED!
              myFindWhat = "~j(?!~j)|(?<!~j)~j",
              myChangeTo = "";
              
              
              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findGrepPreferences.appliedCharacterStyle = myCstyle;
              app.findGrepPreferences.findWhat = myFindWhat;
              app.changeGrepPreferences.changeTo = myChangeTo;
              myDoc.changeGrep();
              
              
              app.findGrepPreferences = app.changeGrepPreferences = null;  
              app.findGrepPreferences.appliedCharacterStyle = myCstyle;
              myFound = myDoc.findGrep();
              
              
              for ( F = 0; F < myFound.length; F++ )  
                  {  
                      var myF = myFound[F];
                      for ( L = 0; L < myF.lines.length-1; L++ )  
                          {  
                              var myLast = myF.lines[L].characters.item(-1);
                              if ( myLast.contents == " " )  myLast.insertionPoints[0].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER;  
                          }
                  }
              }
              

               

               

              No need to have a double if statement and include the last line in the loop!

               

              Cool! Thanks Peter to make me "think different"! 

               

              (^/)

              • 4. Re: Problem with an "if" statement! …
                Peter Kahrel Adobe Community Professional & MVP

                Well, Michel, I must say that I much prefer Jongware's approach. You insert characters in your text, which triggers recomposition and could cause reflow, and you apply GREP styles, which aren't the quickest in longer texts. Jongware's script is much simpler and is probably much quicker too.

                 

                Peter

                • 5. Re: Problem with an "if" statement! …
                  Obi-wan Kenobi Adobe Community Professional

                  Peter,

                   

                  Jongware's way has 2 breaks:

                   

                  1/ it doesn't take care of future modifications of the layout;

                  2/ it doesn't take care of char styles already applied.

                   

                  In a basic situation [without the 2 points above], I'll use it [already saved in my Scripts panel, in Jongware's folder, as "EndLineUnderlining_Jongware.jsx"].

                   

                  … But, in my sample, It couldn't work!!

                   

                  (^/) 

                  • 6. Re: Problem with an "if" statement! …
                    Peter Kahrel Adobe Community Professional & MVP

                    > 2/ it doesn't take care of char styles already applied.

                     

                    It does indeed look as if existing instances aren't removed, but that's easy to fix:

                     

                    1. If the character style exists in the document, delete it.

                    2. Create the character style

                    3. Apply it.

                     

                    That takes care of "it doesn't take care of future modifications of the layout" as well.

                     

                    P.

                    • 7. Re: Problem with an "if" statement! …
                      Obi-wan Kenobi Adobe Community Professional

                      Peter,

                       

                      I've totally written the script. I think my first way can be keeped in some situations.

                       

                      Capture d’écran 2016-12-04 à 03.23.44.png

                       

                      Note that the fourth para won't be taken in account because it's "right-justified"!

                      I launch the script:

                       

                      Capture d’écran 2016-12-04 à 03.24.32.png

                       

                      Note that "Conditions", a char styles group and char styles in it have been created by the script.

                      Conditions underlining can be masked:

                       

                      Capture d’écran 2016-12-04 à 03.25.02.png

                       

                      Important Point: If the layout moves …

                       

                      Capture d’écran 2016-12-04 à 03.26.41.png

                       

                      … I can launch the script again:

                       

                      Capture d’écran 2016-12-04 à 03.28.19.png

                       

                      Masking the Conditions underlining:

                       

                      Capture d’écran 2016-12-04 à 03.28.36.png

                       

                      Script Version:

                       

                      /*
                          Script written by Michel Allio [2016/12/04]
                          See:  https://forums.adobe.com/thread/2245203
                      */
                      
                      
                      app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");    
                          
                      function main ()          
                      {
                          
                      var myDoc = app.activeDocument,
                      myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements(),
                      //  CHARACTER STYLE TO BE DEFINED:  U = Underlined  |  UNo = No-Underlined
                      myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
                      //  Group and Derived Char Styles Automatic Creation:
                      if( myDoc.characterStyleGroups.item( "UNo" ) == null)  myDoc.characterStyleGroups.add({ name: "UNo" }); 
                      for ( U = 0; U < myCstyleU.length; U++ )  if( myDoc.characterStyles.item( myCstyleU[U] + "No" ) == null)  myDoc.characterStyles.add({ name: myCstyleU[U] + "No" }); 
                      //  All The Paras:
                      for ( P = 0; P < myParas.length; P++ )
                          {
                              //  Target: Only Left-Center Alignments Paras:
                              if (myParas[P].justification == Justification.LEFT_ALIGN || myParas[P].justification == Justification.CENTER_ALIGN )
                              {
                                  //  Each Para:
                                  var myPara = myParas[P];
                                  //  Double Char Styles: with and without underlining:
                                  for ( C = 0; C < myCstyleU.length; C++ )
                                      {
                                      var myU = myCstyleU[C];
                                      var myNU = myU + "No";
                                      //  WARNING -- RESET
                                      app.findGrepPreferences = app.changeGrepPreferences = null;  
                                      app.findGrepPreferences.appliedCharacterStyle = myNU;
                                      app.changeGrepPreferences.appliedCharacterStyle = myU;
                                      myPara.changeGrep();
                                      //  New targets = New Underlinings:
                                      app.findGrepPreferences = app.changeGrepPreferences = null;    
                                      app.findGrepPreferences.appliedCharacterStyle = myU;            
                                      myFound = myPara.findGrep();   
                                      //  End of line:
                                      for ( F = 0; F < myFound.length; F++ )
                                          {    
                                              var myF = myFound[F];  
                                              //  Each Line End, except the last line:
                                              for ( L = 0; L < myF.lines.length-1; L++ )    
                                                  {  //  Last char of each line:
                                                      var myLast = myF.lines[L].characters.item(-1);
                                                      //  Deal!  ;-)
                                                      //  -----------------------------------------------------------------------------------------------------------------------------------------
                                                      if ( myLast.contents == " " && myLast.appliedCharacterStyle = myU ) myLast.appliedCharacterStyle = myNU;  
                                                      //  -----------------------------------------------------------------------------------------------------------------------------------------
                                                  }
                                          }
                                      }
                              }   
                          }
                      
                      
                      //  OPTION: Double Char styles Colorization (by Conditions created here! -- Their "underlining" can be masked in the Conditions Panel!)
                      for ( D = 0; D < myCstyleU.length; D++ )
                          {
                              myCondition = app.activeDocument.conditions.item(myCstyleU[D]);
                              if (!myCondition.isValid) myCondition = app.activeDocument.conditions.add ({name: myCstyleU[D], indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
                              
                              app.findGrepPreferences = app.changeGrepPreferences = null;  
                              app.findGrepPreferences.appliedCharacterStyle = myCstyleU[D];
                              app.changeGrepPreferences.appliedConditions = [myCstyleU[D]]; 
                              myDoc.changeGrep();  
                      
                      
                              app.findGrepPreferences = app.changeGrepPreferences = null;  
                              app.findGrepPreferences.appliedCharacterStyle = myCstyleU[D] + "No";
                              app.changeGrepPreferences.appliedConditions = [myCstyleU[D]]; 
                              myDoc.changeGrep();
                          }
                      
                      
                      app.findGrepPreferences = app.changeGrepPreferences = null;  
                      
                      
                      alert("Done!  (^/)")
                      
                      
                      }
                      
                      
                      // ___ (^/) ___
                      

                       

                       

                      Thanks for all comment!  [ … So good for my learning!  ]

                       

                      (^/)

                      • 8. Re: Problem with an "if" statement! …
                        Obi-wan Kenobi Adobe Community Professional

                        Hi,

                         

                        I've detected 2 errors/problems in my script last version and I don't find ways to fix it! 

                         

                        1/ I need to automatically create the char styles group I call "Uno" and the styles into it AND these char styles need to be "based on" their "brother" (same options except the underlining = "none")

                         

                        So I try this:

                         

                        //  CHARACTER STYLE TO BE DEFINED:  U = Underlined  |  UNo = No-Underlined
                        myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
                        //  Group and Derived Char Styles Automatic Creation:
                        if( myDoc.characterStyleGroups.item( "UNo" ) == null)  myDoc.characterStyleGroups.add({ name: "UNo" });
                        var UNoGroup = myDoc.characterStyleGroups.item( "UNo" );
                        for ( U = 0; U < myCstyleU.length; U++ )
                            {
                                if( UNoGroup.characterStyles.item( myCstyleU[U] + "No" ) == null )
                                {
                                    UNoGroup.characterStyles.add({ name: myCstyleU[U] + "No" });   //----------Problem 1:  basedOn: myCstyleU[U]
                                }
                            }
                        

                         

                        "UNo" group and "…Uno" char styles have been correctly created but the styles are not "based on" their brother!!

                         

                        Capture d’écran 2016-12-04 à 15.55.38.png

                         

                        2/ When I launch the script now [the modification of the last version is about what I talk above: the creation of the char styles "into" "Uno" group folder],

                        I get an error: apparently, the new char styles are not detected!

                         

                        Capture d’écran 2016-12-04 à 15.54.35.png

                         

                        Thanks for your help!

                         

                        (^/) 

                        • 9. Re: Problem with an "if" statement! …
                          Obi-wan Kenobi Adobe Community Professional

                          Fixed! … but too tired to go on and search other issues! 

                           

                          /*
                              Script written by Michel Allio [2016/12/04]
                              See:  https://forums.adobe.com/thread/2245203
                          */
                          
                          
                          app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");    
                              
                          function main ()          
                          {
                              
                          var myDoc = app.activeDocument,
                          myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements(),
                          //  CHARACTER STYLE TO BE DEFINED:  U = Underlined  |  UNo = No-Underlined
                          myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
                          //  Group and Derived Char Styles Automatic Creation:
                          if ( myDoc.characterStyleGroups.item( "UNo" ) == null )  myDoc.characterStyleGroups.add({ name: "UNo" });
                          var UNoGroup = myDoc.characterStyleGroups.item( "UNo" );
                          for ( U = 0; U < myCstyleU.length; U++ )
                              {
                                 var  myCsU = myDoc.characterStyles.item(myCstyleU[U]),
                                         myUNo = myCstyleU[U] + "No";
                                 if ( UNoGroup.characterStyles.item( myUNo ) == null )
                                  {
                                     var myCsUNo = UNoGroup.characterStyles.add({ name: myUNo, basedOn: myCsU, underline: false });
                                  }
                              }
                          //  All The Paras:
                          for ( P = 0; P < myParas.length; P++ )
                              {
                                  //  Target: Only Left-Center Alignments Paras:
                                  if ( myParas[P].justification == Justification.LEFT_ALIGN || myParas[P].justification == Justification.CENTER_ALIGN )
                                  {
                                      //  Each Para:
                                      var myPara = myParas[P];
                                      //  Double Char Styles: with and without underlining:
                                      for ( C = 0; C < myCstyleU.length; C++ )
                                          {
                                              var  myCsU = myDoc.characterStyles.item(myCstyleU[C]),
                                                      myUNo = myCsU.name + "No",
                                                      myCsUNo = UNoGroup.characterStyles.item( myUNo );
                                              //  WARNING -- RESET
                                              app.findGrepPreferences = app.changeGrepPreferences = null;  
                                              app.findGrepPreferences.appliedCharacterStyle = myCsUNo;
                                              app.changeGrepPreferences.appliedCharacterStyle = myCsU;
                                              myPara.changeGrep();
                                              //  New targets = New Underlinings:
                                              app.findGrepPreferences = app.changeGrepPreferences = null;    
                                              app.findGrepPreferences.appliedCharacterStyle = myCsU;            
                                              myFound = myPara.findGrep();   
                                              //  End of line:
                                              for ( F = 0; F < myFound.length; F++ )
                                                  {    
                                                      var myF = myFound[F];  
                                                      //  Each Line End, except the last line:
                                                      for ( L = 0; L < myF.lines.length-1; L++ )    
                                                          {  //  Last char of each line:
                                                              var myLast = myF.lines[L].characters.item(-1);
                                                              //  Deal!  ;-)
                                                              //  -----------------------------------------------------------------------------------------------------------------------------------------------
                                                              if ( myLast.contents == " " && myLast.appliedCharacterStyle = myCsU ) myLast.appliedCharacterStyle = myCsUNo;  
                                                              //  -----------------------------------------------------------------------------------------------------------------------------------------------
                                                          }
                                                  }
                                              }
                                      }   
                                  }
                          
                          
                          //  OPTION: Double Char styles Colorization (by Conditions created here! -- Their "underlining" can be masked in the Conditions Panel!)
                          for ( D = 0; D < myCstyleU.length; D++ )
                              {
                                  var   myCsU = myDoc.characterStyles.item(myCstyleU[D]),
                                          myUNo = myCsU.name + "No",
                                          myCsUNo = UNoGroup.characterStyles.item( myUNo ),
                                          myCondition = app.activeDocument.conditions.item(myCstyleU[D]);
                                          
                                  if (!myCondition.isValid) myCondition = app.activeDocument.conditions.add ({name: myCstyleU[D], indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
                                  
                                  app.findGrepPreferences = app.changeGrepPreferences = null;  
                                  app.findGrepPreferences.appliedCharacterStyle = myCsU;
                                  app.changeGrepPreferences.appliedConditions = [myCondition]; 
                                  myDoc.changeGrep();  
                          
                          
                                  app.findGrepPreferences = app.changeGrepPreferences = null;  
                                  app.findGrepPreferences.appliedCharacterStyle = myCsUNo;
                                  app.changeGrepPreferences.appliedConditions = [myCondition]; 
                                  myDoc.changeGrep();
                              }
                          
                          
                          app.findGrepPreferences = app.changeGrepPreferences = null;  
                          
                          
                          alert("Done!  (^/)")
                          
                          
                          }
                          
                          
                          // ___ (^/) ___
                          

                           

                           

                          (^/)