0

How do I do this:

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?

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.

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";
Alex Monthy
  • 1,827
  • 1
  • 14
  • 24
  • It doesn't seem like it will work that way with scripting unless you use a `for` loop to iteratively apply the alternating styles to each paragraph. At least... I couldn't find the supposed "ApplyNextStyle" function in the Javascript reference. – Josh Voigts Oct 07 '13 at 15:10
  • You might be right. But on my first naive try on the subject it worked (or so I believe)! But only once. And I cannot recreate this behaviour, no matter what. I would have thought that the next paragraph style is set by Indesign if the current paragraph has any value set for it, but that does not seem to be the case. -- Well, in the end I did as you suggested and set the character styles explicitly in a loop. – Alex Monthy Oct 14 '13 at 15:57

1 Answers1

0

You can reach the goal by applying objectStyle at the end (key property ==> applyNextParagraphStyle: true)

something like:

var 
 myDocument = app.documents.add(true),
 s1 = myDocument.paragraphStyles.add({name:'one'}),
 s2 = myDocument.paragraphStyles.add({name:'two'}),
 objSt = myDocument.objectStyles.add({
   name: 'AlternateParas',
   enableParagraphStyle: true,
   appliedParagraphStyle: s1,
   applyNextParagraphStyle: true
   }),
 myTextFrame =  myDocument.pages.lastItem().textFrames.add({
  contents: 'abc\rdef\rghi\rklm\r',
  });
with (s1) {nextStyle = s2;}
with (s2) {nextStyle = s1;}
myTextFrame.appliedObjectStyle = objSt;
Cashmirek
  • 269
  • 1
  • 9