5 Replies Latest reply on Aug 23, 2016 6:08 AM by karthiks94217840

    find all line last character and fill color

    karthiks94217840 Level 2

      Dear friend,

       

      I need to find all line last character and fill color (No need to words last character).

       

      My code problem: Without space in the last character is not fill color.

       

      My code is below:

       

      var myDocument = app.activeDocument;  
      var myLine = myDocument.stories.everyItem().lines.everyItem().getElements();  
      for(i=0; i<myLine.length; i++)  
      {  
          var myCon = myLine[i].texts[0].contents;  
          if(myCon.match(/\n| /gi) != null){  
              try{  
                  if(myLine[i].characters.item(-2).fillColor = myDocument.colors[2]);
                  }catch(e){}  
              }  
          }  
      

       

       

      My screenshot also below:

      last.png

       

      Please suggest friend,

       

      Thanks in Advance

      KS

        • 1. Re: find all line last character and fill color
          karthiks94217840 Level 2

          I need output file. Please see below screen shot:

           

          Need.png

          Thanks in Advance

          KS

          • 2. Re: find all line last character and fill color
            Skemicle Level 3

            add an 'if else' statement to apply the fill color to the second to last character only if the last character is a space. else apply the fill color to the last character.

             

            var myDocument = app.activeDocument;   

            var myLine = myDocument.stories.everyItem().lines.everyItem().getElements();   

            for(i=0; i<myLine.length; i++)   

            {   

                var myCon = myLine[i].texts[0].contents;   

                if(myCon.match(/\n| /gi) != null){   

                    try{   

                        if(myLine[i].characters.item(-1).contents == " "){

                            myLine[i].characters.item(-2).fillColor = myDocument.colors[2];

                        }else myLine[i].characters.item(-1).fillColor = myDocument.colors[2];

                    }catch(e){};

                }   

            }

            • 3. Re: find all line last character and fill color
              karthiks94217840 Level 2

              Hi Skemicle,

               

              Thanks to your replay,

               

              Your coding working fine.

               

              One more question: How to match 1st line last character, 2nd line last characters and 3rd line last characters?

               

              Exp:

              Last 1st line character >> a

              Last 2nd line character >> a

              Last 3rd line character >> a

               

              Please suggest friend

               

              Thanks in Advance

              Ks

              • 4. Re: find all line last character and fill color
                Skemicle Level 3

                Your lines are scripted as  myLine[i] for each individual line you will replace the [i] with the index number of each line (keep in mind that javascript starts with an index of 0. (eg. line 1 would be myLine[0],  line 2 would be myLine[1], etc.) This will specify that everything in this line of code will be confined to that line. (eg. myLine[1].characters matches all characters but only within line 2. To determine which character is the same as determining the line, myLine[1].characters[0] being 1st character, to get to the end of a line use the [- ] since javascript is confined to this  line it will loop backwards to the end of the line and you can then set how far from the end of the line you want to match the character (myLine[1].characters[-1] will be the last character, [-2] the second to last, etc.)

                • 5. Re: find all line last character and fill color
                  karthiks94217840 Level 2

                  Hi SK,

                   

                  Thanks to your suggestion.

                   

                  Quick Replay: please go my link and see my code. i am going right way? please suggest.

                   

                  Match last line characters

                   

                   

                  Thanks in Advance

                  KS