Q:

Indesign CC script to apply paragraph styles to multiple paragraphs

I have an Indesign document with the following structure:

paragraph 1 blah blah blah blah blah blah blah blah
paragraph 2 blah blah blah blah blah blah blah blah
paragraph 3 blah blah blah blah blah blah blah blah
paragraph 4 blah blah blah blah blah blah blah blah
paragraph 5 blah blah blah blah blah blah blah blah
. . . and so on...

Now I need to leave the first paragraph as is but apply paragraph styles to all the subsequent paragraphs in the following pattern:

paragraph 2: style A
paragraph 3: style B
paragraph 4: style A
paragraph 5: style B
. . . and so on (alternating pattern)...

I know this can be automated using scripts and I also know a bit of programming in general (JavaScript) but I have no idea how to go about doing this in Indesign. Any suggestion?

A:

Try this script:
provided you have a text frame and you referenced it to a variable myFrame
    for (i=0; i < myFrame.paragraphs.length; i++)
    {
       if ( i%2 == 0 )
       {
        myFrame.parentStory.paragraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item('Style B);
       }
       else
       {
        myFrame.parentStory.paragraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item('Style A);
       }    

    }

Save it as a script in scripts folder and run from the scripts panel. You will need to add frame referencing.

