Skip to main content
Inspiring
July 22, 2022
Answered

How to find text with appliedCharacterStyle inside CharacterStyleGroup?

  • July 22, 2022
  • 1 reply
  • 218 views
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 has been closed for replies.
Correct answer Karthik SG

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
July 22, 2022

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;
            } 
        }
        }
    }