Copy link to clipboard
Copied
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??
Try this
var p = app.selection[0].paragraphs;
var cnt = 0
for (i=0; i<p.length; i++){
if (checkParagraph(p[i]) == 0) {
//the paragraph has text insert cnt at beginning
cnt++
p[i].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){
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences
...
Or are you looking for this, which would require soft returns and no hyphenation:
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].hyph
...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
trying to change line number after empty line to empty line number..
empty line number: 13
next line number: 14 (now new line number should be 13 and get this line content)
Copy link to clipboard
Copied
I lost it, did not understand your latest comment completely. What do you want to do? Lets start from basic.
-Manan
Copy link to clipboard
Copied
Our Karthik is a bit confused. His first question was: how to get the line count of non-empty lines. Now he wants to number the lines but not blank ones.
Copy link to clipboard
Copied
Sorry, It's due to lack of communication. I failed to explain my question correctly! Thanks for the response. I will correct myself😅
Copy link to clipboard
Copied
Hey Manan sorry for the confusion, I am actually trying to insert line number for each line of multiple selected paragraph lines. In between each paragraph I have an enter mark(empty) line which also getting line number inserted. Now I need to skip that line from numbering.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Thanks for making the solution more robust. However, OP seems to have ghosted us so we are as clueless as before as to what is needed afterall 🙂
-Manan
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Try this
var p = app.selection[0].paragraphs;
var cnt = 0
for (i=0; i<p.length; i++){
if (checkParagraph(p[i]) == 0) {
//the paragraph has text insert cnt at beginning
cnt++
p[i].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){
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "^\\s*\r";
var a = p.findGrep();
app.findGrepPreferences = NothingEnum.nothing;
return a.length;
}
But what‘s the reason for the empty return? Why not remove the returns, and use a Space After and a Numbered List?
Copy link to clipboard
Copied
Or are you looking for this, which would require soft returns and no hyphenation:
Copy link to clipboard
Copied
Yes I am looking for this only!
Copy link to clipboard
Copied
But adding text to existing lines could change the line endings. Like for example adding "1. " would add three characters so the line ending will shift by three characters. What sense it would make then? If we add soft return before adding the characters then also it will cause a mess as the 2 characters and soft return would wrap on to new line I suppose.
-Manan
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Maybe place a separate text frame to the left of the frame with the content and place the numbers there.
P.
Copy link to clipboard
Copied
As you think, I am adding it in separate textFrame. So no issues with that!
Copy link to clipboard
Copied
Yes! I too noticed while trying to insert in starting of each line. For this purpose what you explained is absolutely correct. thanks for your response and time @rob day
Copy link to clipboard
Copied
Wouldn't using paragraph spacing instead of extra returns solve the count issue? It's a better InDesign workflow too.
Copy link to clipboard
Copied
Absolutely.