• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

find all line last character and fill color

Contributor ,
Aug 22, 2016 Aug 22, 2016

Copy link to clipboard

Copied

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.texts[0].contents; 

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

        try{ 

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

            }catch(e){} 

        } 

    } 

My screenshot also below:

last.png

Please suggest friend,

Thanks in Advance

KS

TOPICS
Scripting

Views

468

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Aug 22, 2016 Aug 22, 2016

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.texts[0].contents;   

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

        try{   

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

         

...

Votes

Translate

Translate
Contributor ,
Aug 22, 2016 Aug 22, 2016

Copy link to clipboard

Copied

I need output file. Please see below screen shot:

Need.png

Thanks in Advance

KS

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 22, 2016 Aug 22, 2016

Copy link to clipboard

Copied

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.texts[0].contents;   

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

        try{   

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

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

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

        }catch(e){};

    }   

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 23, 2016 Aug 23, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 23, 2016 Aug 23, 2016

Copy link to clipboard

Copied

Your lines are scripted as  myLine for each individual line you will replace the 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.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 23, 2016 Aug 23, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines