Skip to main content
Inspiring
February 15, 2022
Answered

how to skip enter lines(empty lines) count between no of lines selection in indesign

  • February 15, 2022
  • 2 replies
  • 1671 views
var myText = app.selection[0].lines;
var b;
var c=0;
for (i=0; i<myText.length; i++){     
  if(myText[i].characters[0].contents ==0){
      c =i-1;
      break;
      }                      
  if (c != 0){
      b=i-1;
      }
  else{
          b=i+1;
      }
  if(b % 5 === 0){
   var myInsertion= myText[i].insertionPoints[0];
   var mycontent = myText[i].contents
    }
}

is there any way to count length of lines by skipping empty lines in indesign??

This topic has been closed for replies.
Correct answer rob day

Now this works for me

var p = app.selection[0].lines;
var cnt = 0
for (i=0; i<p.length-1; i++){     
    if (checkLine(p[i]) == 0) {
        cnt++;
        p[i].insertionPoints[0].contents = cnt + ". ";
    } 
}
function checkLine(p){
    app.findGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = "^\\s*\r";
    var a = p.findGrep();
    app.findGrepPreferences = NothingEnum.nothing;
    return a.length;
}

Hi @Karthik SG , for that to work your "lines" must be single line paragraphs? This is what I get when the paragraphs have more than one line, which is what your original post was showing:

 

 

 

This version works by inserting a forced line break at the end of the line above, but you would have a mess if you need to edit after running the script:

 

 

 

var p = app.selection[0].paragraphs;
var lns;
var cnt = 0

for (i=0; i<p.length; i++){     
    if (checkParagraph(p[i])==0) {
        p[i].hyphenation = false;
        cnt++
        lns = p[i].lines;
        for (var j = 0; j < lns.length-1; j++){
            lns[j].insertionPoints[0].contents = cnt + ". ";
            lns[j].insertionPoints[-1].contents = SpecialCharacters.FORCED_LINE_BREAK;
            cnt++
        }; 
        lns.lastItem().insertionPoints[0].contents = cnt + ". ";
    } 
}


/**
* Check for empty paragraphs 
* @param paragraph to check 
* @return grep array length, 1 if the paragraph is empty
* 
*/
function checkParagraph(p){
    var isEmpty = 1
    try {
        app.findGrepPreferences = NothingEnum.nothing;
        app.findGrepPreferences.findWhat = "^\\s*\r";
        a = p.findGrep();
        isEmpty = a.length
        app.findGrepPreferences = NothingEnum.nothing;
    }catch(e) {}  
    return isEmpty;
}

 

 

 

 

2 replies

Dave Creamer of IDEAS
Community Expert
Community Expert
February 19, 2022

Wouldn't using paragraph spacing instead of extra returns solve the count issue? It's a better InDesign workflow too. 

David Creamer: Community Expert (ACI and ACE 1995-2023)
Community Expert
February 21, 2022

Absolutely.

Community Expert
February 15, 2022

Do you want to count the number of non empty lines in the selection? If so try the following

app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "^\r";
var a = app.selection[0].findGrep();
alert(app.selection[0].lines.length - a.length)
app.findGrepPreferences = NothingEnum.nothing;

-Manan

-Manan
rob day
Community Expert
Community Expert
February 19, 2022

Hi Manan, a line could have white space plus the return, so @Karthik SG might need to search for lines with any white space plus the return. So maybe this:

 

app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "^\\s*\r";
var a = app.selection[0].findGrep();
alert("The selected " + app.selection[0].lines.length + " lines contains " + a.length + " empty line")
app.findGrepPreferences = NothingEnum.nothing;

 

 

Inspiring
February 23, 2022
  • Hey Rob, thanks for the response! I am trying to insert line number for already selected n number of lines to each line which has content and empty line should not be inserted with line number.. with your example,
  • This is I am getting so far, 
  • 1.Hello
  • 2.
  • 3.there
  • What I actually want is, 
  •    1. Hello
  •  
  •     2.there