title: Scripting Indesign: alternating paragraph styles
body: <p>How do I do this:</p>
  
  <p>I have a document with two alternating paragraph styles, s1 and s2. s1 is followed by s2 and vice versa. This works fine with the user interface. But how does it work with scripting?</p>
  
  <p>In the following code sample all text gets formatted as paragraph style s1 instead of alternating the styles. When you add paragraphs using the user interface, the paragraph format alternates as desired.</p>
  
  <pre><code>var myDocument = app.documents.add(true);
  var s1 = myDocument.paragraphStyles.add({name:'one'});
  var s2 = myDocument.paragraphStyles.add({name:'two'});
  with (s1) {nextStyle = s2;}
  with (s2) {nextStyle = s1;}
  
  var myTextFrame =  myDocument.spreads.lastItem().pages.lastItem().textFrames.add();
  myTextFrame.geometricBounds = ["20mm","20mm","100mm","100mm"];
  myTextFrame.parentStory.insertionPoints.item(0).appliedParagraphStyle = s1;
  myTextFrame.contents = "abc\rdef\rghi\rklm\r";
  </code></pre>