4 Replies Latest reply on Oct 22, 2016 7:13 AM by Obi-wan Kenobi

    Code to be evaluated! [002]

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters! 

       

      I want to move the 3rd para at the end of each frame! … but only if the "Blue_Para" para style is applied to its 1rst para!

      [ … to avoid a true mess in the doc!  ]

       

      Capture d’écran 2016-10-22 à 12.33.20.png

      Capture d’écran 2016-10-22 à 12.27.49.png

       

      The code below works fine! … but the script triggers a weird alert:

       

      Capture d’écran 2016-10-22 à 12.27.18.png

       

      The problem seems to be limited to the "if" statement! … but I don't understand why!

      If I click on "OK", the script goes on and the result is fine! 

      [ … Just a few centuries and, with the help of The Force, I'll become a Script Master! ]

       

       

      var myTFrames = app.activeDocument.textFrames;
      
      
      for ( t = 0 ; t < myTFrames.length; t++ )
          {
              var myTFrame = myTFrames[t];
              
              if  (  myTFrame.paragraphs[0].appliedParagraphStyle.name == "Blue_Para"  )
                  {
                      myTFrame.characters[-1].contents = "\r";  
                      myTFrame.paragraphs[2].move(LocationOptions.AT_END);  
                      myTFrame.characters[-1].contents = "";  
                  }
          }
      

       

      [ Lines 10-12, courtesy of Skemicle // Sort paragraphs in a certain way  ]

       

      Thanks in advance!

      (^/)

        • 1. Re: Code to be evaluated! [002]
          tpk1982 Level 4

          Hi Obi,

           

          I tried the coding and working fine. I don't get any error message. If it comes for you then please use try catch method

           

          Regards,

          K

          1 person found this helpful
          • 2. Re: Code to be evaluated! [002]
            Obi-wan Kenobi Adobe Community Professional

            Thanks Kartik! 

             

            I did it before posting:

             

            var myTFrames = app.activeDocument.textFrames;
            
            
            for ( t = 0 ; t < myTFrames.length; t++ )
                {
                    var myTFrame = myTFrames[t];
                    try
                        {
                            if  (  myTFrame.characters[0].appliedParagraphStyle.name == "Blue_Para"  )
                                {
                                    myTFrame.characters[-1].contents = "\r";  
                                    myTFrame.paragraphs[2].move(LocationOptions.AT_END);  
                                    myTFrame.characters[-1].contents = "";  
                                }
                        }
                    catch (myError) { }
            }
            

             

            This way only hides the problem under the carpet! 

             

            … I don't want to be boring [ ] but this's not very satisfactory and it doesn't explain me why I encounter myself such a mistake and you not!!

             

            (^/) 

            • 3. Re: Code to be evaluated! [002]
              [Jongware] Most Valuable Participant

              After running your code, the last character of my test strings disappeared Change the add-a-return line to

               

              myTFrame.insertionPoints[-1].contents = "\r";

               

              to avoid that.

               

              I am able to trigger the exact same error if there is an empty text frame anywhere in your document. To prevent that, change the 'if' test to

               

              if  (myTFrame.paragraphs.length > 0 && myTFrame.paragraphs[0].appliedParagraphStyle.name == "Blue_Para" )

               

              If that does not work, add this to the 'catch' code:

               

              myTFrame.select();

               

              so you can see which frame causes the problem.

              1 person found this helpful
              • 4. Re: Code to be evaluated! [002]
                Obi-wan Kenobi Adobe Community Professional

                Thanks Jongware!  Empty text frames on Master Pages!!

                 

                Final script version saved in my Scripts Panel:

                 

                /*
                    See: "Code to be evaluated! [002]" [2016/10/22] [https://forums.adobe.com/thread/2225693]
                    Script written by Michel Allio
                    Lines 23-24-25, courtesy of Skemicle | See: "Sort paragraphs in a certain way" [https://forums.adobe.com/thread/2225033]
                    Line 21-23, corrected by Jongware
                */
                
                
                app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, 'Move Paras! …');
                  
                function main()
                {  
                
                
                var myTFrames = app.activeDocument.textFrames;
                
                
                for ( t = 0 ; t < myTFrames.length; t++ )
                    {
                        var myTFrame = myTFrames[t];
                        if  (  myTFrame.paragraphs.length > 0 && myTFrame.paragraphs[0].appliedParagraphStyle.name == "Blue_Para"  )
                            {
                                myTFrame.insertionPoints[-1].contents = "\r";
                                myTFrame.paragraphs[2].move(LocationOptions.AT_END);  
                                myTFrame.characters[-1].contents = "";  
                            }
                    }
                
                
                }
                

                 

                 

                No real actual use but very good for my [JS] learning!

                 

                Thanks all!

                 

                (^/)