Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script find - paragraph style (under the folder)

Explorer ,
Mar 24, 2022 Mar 24, 2022

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

 

Capture.PNG

 

Thanks,

Magesh

TOPICS
Scripting
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 24, 2022 Mar 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.allParagraphS
...
Translate
Community Expert , Mar 24, 2022 Mar 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 s

...
Translate
Community Expert ,
Mar 24, 2022 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 25, 2022 Mar 25, 2022

Hi Mark,

 

Thanks for your support!!

 

Regards,

Magesh

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2022 Mar 26, 2022

@m1b 

Mark -- It's not a bug, it's the way it was implemented deliberately. myDoc.paragraphStyles returns the level-1 paragraph styles (as a collection), myDoc.allParagraphStyles returns all paragraph styles (as an array). As Uwe mentions, you can have several paragraph styles with the same name in different groups, which is disaster or a blessing, depending on who you ask. It's the same for character, object, table, and cell styles.

With colours Adobe took a different route: you have colour groups, but you can't have duplicate colour names. And myDoc.colors returns a collection of all colours.

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2022 Mar 26, 2022
LATEST

Thanks Peter, yes I see it is more nuanced than I thought. Just another gotcha in any case.

- Mark

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2022 Mar 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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 25, 2022 Mar 25, 2022

Thanks Uwe Laubender

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines