3 Replies Latest reply on Jun 28, 2012 7:12 AM by Learner X

    Apply color last character of line

    Learner X Level 1

      Hi ALL,

       

      I am the beginner in Javascript.

       

       

      Question: I want  apply color to the last character of the line. It not works.

       

       

       

      //My lines are follows:

       

      var myDocument = app.activeDocument;

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

       

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

      {

          myLine[i].characters.item(-1).fillColor = "SS";

          }

       

      I think error held in item(-1) occurance only.

       

      Can anyone give the solution?

       

       

       

      /*But if i want to apply color to the beginning of the line it works.

       

       

      This is the line i follows:

       

      var myDocument = app.activeDocument;

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

       

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

      {

          myLine[i].characters.item(0).fillColor = "SS";

          }*/

        • 1. Re: Apply color last character of line
          absqua Level 4

          Looks like it should work. Probably the last character of every line is whitespace of some kind and you just don't see that the swatch is applied.

           

          Jeff

          • 2. Re: Apply color last character of line
            csm_phil Level 4

            Hi X,

             

            You have to check the lined end of the characters is unwanted space, enter mark, shift enter, etc..

             

            Please try the below JS code.

             

             

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

             

             

            thx

            csm_phil

            • 3. Re: Apply color last character of line
              Learner X Level 1

              Hi csm_phil/All,

               

              Thanks for your reply...

               

              Its working fine.

               

              But i have doubt in that above script: I am a beginner and learner through forum and scripting tutorials

               

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

               

              1. why we use match ("myCon.match"). I search scripting reference guide but i cant get it.

               

              2.  \r = enter mark why we use ("/\r)

               

              3. \n = shift enter but we not use "/" like enter mark

               

              4. what is /gi.

               

               

              Can you or anyone explain it?

               

              Because i want to learn.