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;
}
