Skip to main content
Inspiring
July 7, 2023
Answered

Trying to format a bullet list in Indesign with a paragraph style sheet

  • July 7, 2023
  • 2 replies
  • 452 views

Dear all,

I have a text frame and a few paragraphs in it including a very few paragraphs are formatted as bullet list when the doc file is imported in that text frame. Following code check all paragraphs and if a para is formatted as bullet list then it apply "xyz" stylesheet. 

 

var myDoc = app.activeDocument;
var i;
var myFrames22 = myDoc.textFrames.itemByName("India");
var my_count = myFrames22.parentStory.paragraphs.length;
for(i = my_count; i > 0; i--){
      if(myFrames22.parentStory.paragraphs.item(i).bulletsAndNumberingListType == ListType.BULLET_LIST) {  // i receive error here
 
myFrames22.parentStory.paragraphs[i].appliedParagraphStyle = "xyz";
}
}
This topic has been closed for replies.
Correct answer brian_p_dts

You were trying to reference an out of bounds part of the paragraph array by not setting my_count to length - 1. Here's an easier way to reverse iterate through an array. We subtract one from i at the start of the loop to ensure we're hitting the last actual element in the array, and this will also consider the 0 index of the array. If you wanted your for loop to work, it would have been: 

for(i = my_count - 1; i >= 0; i--)

 

var myDoc = app.activeDocument;
var myFrames22 = myDoc.textFrames.itemByName("India");
var i = myFrames22.parentStory.paragraphs.length;
while(i--){
      if(myFrames22.parentStory.paragraphs.item(i).bulletsAndNumberingListType == ListType.BULLET_LIST) {  // i receive error here
 
myFrames22.parentStory.paragraphs[i].appliedParagraphStyle = "xyz";
}
}

 

2 replies

Inspiring
July 7, 2023

Error 45

Object invalid 

if(myFrames22.parentStory.paragraphs.item(i).bulletsAndNumberingListType == ListType.BULLET_LIST) { // i receive error here

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
July 7, 2023

You were trying to reference an out of bounds part of the paragraph array by not setting my_count to length - 1. Here's an easier way to reverse iterate through an array. We subtract one from i at the start of the loop to ensure we're hitting the last actual element in the array, and this will also consider the 0 index of the array. If you wanted your for loop to work, it would have been: 

for(i = my_count - 1; i >= 0; i--)

 

var myDoc = app.activeDocument;
var myFrames22 = myDoc.textFrames.itemByName("India");
var i = myFrames22.parentStory.paragraphs.length;
while(i--){
      if(myFrames22.parentStory.paragraphs.item(i).bulletsAndNumberingListType == ListType.BULLET_LIST) {  // i receive error here
 
myFrames22.parentStory.paragraphs[i].appliedParagraphStyle = "xyz";
}
}

 

Inspiring
July 10, 2023

Hi Brian, It worked like a charm. My another learning is: I should look the code from general perpsective (here the error is due to array out of bounds) as well not only from InDesign related code perspective.

brian_p_dts
Community Expert
Community Expert
July 7, 2023

What error do you receive?