Skip to main content
Inspiring
September 16, 2016
Answered

GREP find paragraph style

  • September 16, 2016
  • 1 reply
  • 1276 views

Hi guys,

I'm using this grep to find all words that use the Body text paragraph style.

However I get the same result whether I specify a paragraph style to find or not.

The result is always the same. Why is this?

app.findGrepPreferences=null;

var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";

found.appliedParagraphStyle="Body text";

found=app.activeDocument.findGrep();

alert("This document has "+found.length+" words");

gives the same result as

app.findGrepPreferences=null;

var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";

found=app.activeDocument.findGrep();

alert("This document has "+found.length+" words");

This topic has been closed for replies.
Correct answer Obi-wan Kenobi

app.findTextPreferences.appliedParagraphStyle=app.activeDocument.paragraphStyleGroups.itemByName("body").paragraphStyles.item("Body Text");


app.findGrepPreferences=null;

var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";

app.findGrepPreferences.appliedParagraphStyle=app.activeDocument.paragraphStyleGroups.item("body").paragraphStyles.item("Body text");

found=app.activeDocument.findGrep();

alert("This document has "+found.length+" words");

(^/)

1 reply

TᴀW
Legend
September 16, 2016

You're not doing it quite right:

app.findGrepPreferences = null;

app.findGrepPreferences... etc. (this is fine, but you don't need to assign the found variable yet, it achieves nothing to do so)

app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Body text"); // Could be that you can use your shortened form, but I always do it the long way, so I know exactly what's happening. Also, for para styles in Groups it's necessary.

found = app.activeDocument.findGrep();

etc.

Ariel

Edit: Specifically, in your line beginning var found =, you have actually assigned to the found variable the string "\\b\\w+?\\b", NOT app.findGrepPreferences.findWhat, which it looks like you were trying to do...

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
Inspiring
September 16, 2016

Hi Ariel,

when I run this code:

app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Body text");

this line returns an error - "invalid value for set property 'appliedParagraphStyle'. Expected string, paragraphStyle but received nothing.

tpk1982
Legend
September 16, 2016

Make sure your style is not within any group, otherwise we need to give the group name in the coding