Skip to main content
Inspiring
Mar 24, 2022 8:47 AM UTC
Answered

Script find - paragraph style (under the folder)

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

LaubenderCorrect answer
Community Expert
Mar 24, 2022 10:50 AM UTC

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 )

6 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
Mar 24, 2022 10:50 AM UTC

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 )

Translate
Inspiring
Mar 26, 2022 6:49 AM UTC

Thanks Uwe Laubender

Translate
m1b
Community Expert
Community Expert
Mar 24, 2022 10:27 AM UTC

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

Translate
Inspiring
Mar 25, 2022 9:01 AM UTC

Hi Mark,

 

Thanks for your support!!

 

Regards,

Magesh

Translate