Skip to main content
Inspiring
March 24, 2022
Answered

Script find - paragraph style (under the folder)

  • March 24, 2022
  • 2 replies
  • 2935 views

Hi All,

I have one doubt. Please help me.

Find text with paragraph style, we use the below code:

app.findTextPreferences.appliedParagraphStyle = "Paragraph Style Name";

If I find the text with paragraph under the folder like below image. Above code not working. So please help me

 

 

Thanks,

Magesh

Correct answer Laubender

Hi Mageshwaran,

you could also implement it by using the "Style Group 1".

var paraStyle = app.documents[0].paragraphStyleGroups.itemByName( "Style Group 1" ).
paragraphStyles.itemByName( "find this style" );

app.findTextPreferences.appliedParagraphStyle = paraStyle;

 

If you have to search through all your paragraph styles, because it's unknown if the style is in a style group, then search the document's allParagraphStyles array like Mark suggested. But note, that there could be more than one style with the same name in a different style group.

 

That brings me to another thing. If you already know one single text where the paragraph style is applied to or perhaps the user has a selection of that text, work with that text or that selection:

 

app.findTextPreferences.appliedParagraphStyle =
app.selection[0].appliedParagraphStyle;

 

Regards,
Uwe Laubender

( ACP )

2 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
March 24, 2022

Hi Mageshwaran,

you could also implement it by using the "Style Group 1".

var paraStyle = app.documents[0].paragraphStyleGroups.itemByName( "Style Group 1" ).
paragraphStyles.itemByName( "find this style" );

app.findTextPreferences.appliedParagraphStyle = paraStyle;

 

If you have to search through all your paragraph styles, because it's unknown if the style is in a style group, then search the document's allParagraphStyles array like Mark suggested. But note, that there could be more than one style with the same name in a different style group.

 

That brings me to another thing. If you already know one single text where the paragraph style is applied to or perhaps the user has a selection of that text, work with that text or that selection:

 

app.findTextPreferences.appliedParagraphStyle =
app.selection[0].appliedParagraphStyle;

 

Regards,
Uwe Laubender

( ACP )

Inspiring
March 26, 2022

Thanks Uwe Laubender

m1b
Community Expert
Community Expert
March 24, 2022

Hi @Mageshwaran, what you typed isn't wrong—it just doesn't work. For some strange reason styles in folders can't be accessed that way. It's a bug in my opinion. Here is a workaround:

function getByName(styles, name) {
    for (var i = 0; i < styles.length; i++)
        if (styles[i].name == name) return styles[i];
}

var doc = app.activeDocument;
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = getByName(doc.allParagraphStyles, "find this style");
var result = doc.findText();

 

What I am doing differently here is getting the paragraph style explicitly and using that to match the appliedParagraphStyle rather than just sending a string of the name of the style. The getByName function just looks through all the styles until the name supplied matches (it works with any styles array, eg. character or cell or table styles, too).

- Mark

Inspiring
March 25, 2022

Hi Mark,

 

Thanks for your support!!

 

Regards,

Magesh