Skip to main content
Inspiring
March 21, 2013
Answered

Select all paragraphs of a certain style without looping

  • March 21, 2013
  • 1 reply
  • 3644 views

Did a quick search so please forgive me if this is a duplicate.

var  paragraphs = app.activeDocument.textFrames.everyItem().paragraphs.everyItem();

Without looping through every paragraph is there a way to reference paragraphs with a certain applied style?

I'm thinking something like this:

var headlines = paragraphs.itemsByAppliedParagraphStyle("Headline 1");

//headlines is an array of all paragraph which have the paragraph style "Headline 1" applied to them

Thank-you for any help you can provide.

This topic has been closed for replies.
Correct answer Trevor:

Just use findGrep or findText like this

app.findGrepPreferences = null

app.findGrepPreferences.appliedParagraphStyle = "Headline 1"

myHeadlineParagraphs = app.activeDocument.findGrep()

Trevor

1 reply

Trevor:
Trevor:Correct answer
Legend
March 21, 2013

Just use findGrep or findText like this

app.findGrepPreferences = null

app.findGrepPreferences.appliedParagraphStyle = "Headline 1"

myHeadlineParagraphs = app.activeDocument.findGrep()

Trevor

Jongware
Community Expert
Community Expert
March 22, 2013

Totally annoying

I was thinking, "why without looping"? After all, at some point either you or InDesign has to "loop" over all paragraphs -- even if it is only inside the Search function. So I gathered KuddRoww only had the impression 'without looping' would somehow be 'faster' than 'with looping', in Javascript.

However ... although using getElements is very fast, it's the Looping that you need afterwards (inspecting each style and discarding the bad ones) that takes most of the time. With some timing on a 200 pp long document, this looping costs me 0.31 seconds, while Trevor's findGrep solution takes 0.111 seconds -- a three-times improvement!

Oh well.

Only thing is, if you want a list of all separate headline paragraphs, you might want to add this line to the findGrepPreferences:

app.findGrepPreferences.findWhat = "(?s)^.*?\r";

This is to prevent it from picking up more than one consecutive paragraph as a single "hit". (I know, with headlines it should probably not occur. But still.) It took some time to find the correct syntax, because the "^" "Start of Paragraph" also reacts to soft line breaks, and then you'd find this 'paragraph' twice. Using the combination of single line, shortest match, and Always End with Return, should make it find only 'entire paragraphs'. (I might have to come back on that though.)

Jongware
Community Expert
Community Expert
March 22, 2013

[Jongware] wrote:

(stuff) ... (I might have to come back on that though.)

Yup. Of course that final hard return should be optional, because the very final paragraph in a story might not end with one. Adding a "?" at the end, though, made my InDesign crash.

So this alternative findWhat will match only 'entire paragraphs':

[^\r]+

whether or not it contains soft line breaks.