Skip to main content
Inspiring
Jul 22, 2022 5:15 AM UTC
Answered

How to find text with appliedCharacterStyle inside CharacterStyleGroup?

var doc = app.activeDocument;
var doc_allParaStyles = doc.allParagraphStyles;
for(var p = 0 ; p < doc_allParaStyles.length ; p++){ 
    var AllNestedStyles = doc_allParaStyles[p].nestedStyles;
     for(var n=0; n<AllNestedStyles.length;n++){
            var NestedStyleName=AllNestedStyles[n].appliedCharacterStyle.name;
            app.findTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = NestedStyleName;
      //here how to find text with aplliedCharacterStyle inside characterStyleGroups!
            var found_txt = app.activeDocument.findText();
            app.findTextPreferences = NothingEnum.nothing;
            for(var a =0 ; a < found_txt.length ; a++)
             {
                var foundCon = found_txt[a].contents;
            } 
        }
    }

In the above code, when I try to find any characterStyleName inside the characterStyleGroup, it is shows following error:

Error: Invalid value for set property 'appliedCharacterStyle'. Expected String, CharacterStyle or NothingEnum enumerator, but received "c4H".

Is there anyother way to find with allCharacterStyles (including groups)! 

This topic is closed to new replies. Start a new post to keep the conversation going.
Karthik SGCorrect answer
Inspiring
Jul 22, 2022 8:43 AM UTC

found the way! its working now..

var doc = app.activeDocument;
var doc_allParaStyles = doc.allParagraphStyles;
for(var p = 0 ; p < doc_allParaStyles.length ; p++){ 
    var AllNestedStyles = doc_allParaStyles[p].nestedStyles;
     for(var n=0; n<AllNestedStyles.length;n++){
        if(AllNestedStyles[n].appliedCharacterStyle.parent == "[object CharacterStyleGroup]"){
            var charStyleGroupName = AllNestedStyles[n].appliedCharacterStyle.parent;
            var NestedStyleName=AllNestedStyles[n].appliedCharacterStyle.name;
            cstylegroup = app.activeDocument.characterStyleGroups.itemByName(charStyleGroupName.name);
            app.findTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = cstylegroup.characterStyles.itemByName(NestedStyleName);
            var found_txt = app.activeDocument.findText();
            app.findTextPreferences = NothingEnum.nothing;
            for(var a =0 ; a < found_txt.length ; a++)
             {
                var foundCon = found_txt[a].contents;
            }
        }
        else{
            var NestedStyleName=AllNestedStyles[n].appliedCharacterStyle.name;
            app.findTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = NestedStyleName;
            var found_txt = app.activeDocument.findText();
            app.findTextPreferences = NothingEnum.nothing;
            for(var a =0 ; a < found_txt.length ; a++)
             {
                var foundCon = found_txt[a].contents;
            } 
        }
        }
    }

1 reply

Karthik SGAuthorCorrect answer
Inspiring
Jul 22, 2022 8:43 AM UTC

found the way! its working now..

var doc = app.activeDocument;
var doc_allParaStyles = doc.allParagraphStyles;
for(var p = 0 ; p < doc_allParaStyles.length ; p++){ 
    var AllNestedStyles = doc_allParaStyles[p].nestedStyles;
     for(var n=0; n<AllNestedStyles.length;n++){
        if(AllNestedStyles[n].appliedCharacterStyle.parent == "[object CharacterStyleGroup]"){
            var charStyleGroupName = AllNestedStyles[n].appliedCharacterStyle.parent;
            var NestedStyleName=AllNestedStyles[n].appliedCharacterStyle.name;
            cstylegroup = app.activeDocument.characterStyleGroups.itemByName(charStyleGroupName.name);
            app.findTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = cstylegroup.characterStyles.itemByName(NestedStyleName);
            var found_txt = app.activeDocument.findText();
            app.findTextPreferences = NothingEnum.nothing;
            for(var a =0 ; a < found_txt.length ; a++)
             {
                var foundCon = found_txt[a].contents;
            }
        }
        else{
            var NestedStyleName=AllNestedStyles[n].appliedCharacterStyle.name;
            app.findTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = NestedStyleName;
            var found_txt = app.activeDocument.findText();
            app.findTextPreferences = NothingEnum.nothing;
            for(var a =0 ; a < found_txt.length ; a++)
             {
                var foundCon = found_txt[a].contents;
            } 
        }
        }
    }
Translate