Skip to main content
October 4, 2011
Answered

For loop stops iterating at first page

  • October 4, 2011
  • 1 reply
  • 761 views

I have a loop that applies a paragraph style but it stops iterating at the first page of the document.

I need it to apply the style to the entire document...can't figure out why this isn't working.

Here is my script:

var myDocument = app.activeDocument;

var myParagraphStyle = myDocument.paragraphStyles.item("Paragraph Style");

myDocument.pages.item(0).textFrames.item(0).paragraphs.item(0).appliedParagraphStyle = myParagraphStyle;
var  paragraphs = myDocument.pages.item(0).textFrames.item(0).paragraphs

for( i = 0; i < paragraphs.length; i++)
{
     var paragraph = paragraphs.item(i)
     
     
     paragraph.applyParagraphStyle(myParagraphStyle, true);
}

Thank you

This topic has been closed for replies.
Correct answer j-per

Thanks, I cleaned up the script,  but still it stops after the first page.

Yes, because you talk to the paragraphs in a special (first) textFrame and not to the story.

You must change your third line or replace with that:

var  myParagraphs = myDocument.pages.item(0).textFrames.item(0).parentStory.paragraphs;

1 reply

Participant
October 4, 2011

myDocument.pages.item(0).textFrames.item(0).paragraphs.item(0).applied ParagraphStyle = myParagraphStyle;

Why do you have this line, when you iterate through the paragraphs later? (BTW: between "applied" and "ParagraphStyle" is a space)

But I think the problem is that you forgot two semicolon here:

var  paragraphs = myDocument.pages.item(0).textFrames.item(0).paragraphs

(BTW: you should also call it "var myParagraphs ..." or something like that.)

and here:

var paragraph = paragraphs.item(i)

October 4, 2011

Thanks, I cleaned up the script,  but still it stops after the first page.

Here's my code now:

var myDocument = app.activeDocument;

var myParagraphStyle = myDocument.paragraphStyles.item("A10 EN Normal");

var  myParagraphs = myDocument.pages.item(0).textFrames.item(0).paragraphs;


for( i = 0; i < myParagraphs.length; i++)
{
     var paragraph = myParagraphs.item(i);
     
     
     paragraph.applyParagraphStyle(myParagraphStyle, true);
}

j-perCorrect answer
Participant
October 4, 2011

Thanks, I cleaned up the script,  but still it stops after the first page.

Yes, because you talk to the paragraphs in a special (first) textFrame and not to the story.

You must change your third line or replace with that:

var  myParagraphs = myDocument.pages.item(0).textFrames.item(0).parentStory.paragraphs;