Skip to main content
September 14, 2010
Question

Differences in Paragraph/Text Style Ranges in CS3/4

  • September 14, 2010
  • 2 replies
  • 601 views

I have code that exports the text from a frame into "block" and "inline" element. The code written for CS3 looks something like this below:

var doc = app.activeDocument;

var pageItems = doc.pageItems;

for (var i = 0; i < pageItems.count(); i++) {

  var pageItem = pageItems.item(i);

  var textFrame = pageItem;

  $.writeln("Number of Paragraphs: " + textFrame.paragraphs.count());

  for (var j = 0; j < textFrame.paragraphs.count(); j++) {

    handleParagraph(textFrame.paragraphs.item(j), j);

  }

}


function handleParagraph(para, count) {

  if (para.constructor.name == "Story" ) {

    $.writeln("Paragraph is a Story");

  }

  else {

    for (var i = 0; i < para.textStyleRanges.count(); i++) {

      var text = para.textStyleRanges.item(i);

      $.writeln("PARAGRAPH[" + (count+1) + "] has text [" + para.contents + "]" );

    }

  }

}

'';

When you have 2 paragraphs of text that contain a single sentence in the default text format, this outputs something like:

Number of Paragraphs: 2

PARAGRAPH[1] has text [Paragraph 1

]

PARAGRAPH[2] has text [Paragraph 2]

However, when I run the same code against a document in CS4, I get:

Number of Paragraphs: 2

PARAGRAPH[1] has text [Paragraph 1

Paragraph 2]

PARAGRAPH[2] has text [Paragraph 2]

So the contents of the first text range in paragraph #1 under CS3 stops at the end of paragraph 1, while in CS4, it contains ALL of the contents of the frame (that is of the same style). The content of paragraph #2 starts with the text in paragraph #2.
Is there a "correct" method for doing what I'm attempting here? Or at least some way of accomplishing this that should work in BOTH CS3 and CS4 (and CS5 as well I suppose)?
Thanks!
-Patrick
This topic has been closed for replies.

2 replies

Harbs.
Legend
September 14, 2010

Yes. I'm pretty sure this is a bug (one that caused me a lot of grief!). I think it's still there in CS5. You need to check that it's part of the same paragraph...

Harbs

Jongware
Community Expert
Community Expert
September 14, 2010

It's because of this line

var text = para.textStyleRanges.item(i);

-- and, correct, it seems its behaviour has been changed somewhere along the line. Suddenly, a textStyleRange can cross over from one paragraph into the next one if (and only if) the same formatting continues.

Try searching this forum on textStyleRanges -- I could fix my script when the same occurred to me but I can't recall how