Copy link to clipboard
Copied
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
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
...
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Mark,
Thanks for your support!!
Regards,
Magesh
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks Peter, yes I see it is more nuanced than I thought. Just another gotcha in any case.
- Mark
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
Thanks Uwe Laubender
Find more inspiration, events, and resources on the new Adobe Community
Explore Now