7 Replies Latest reply on Dec 20, 2012 10:58 PM by Vamitul

    Creating a new characterStyle from text

    vincent 1982 Level 1

      Hello,

       

      I'm working on a plugin and i need to remove all the "[None]" character styles from a document.

       

      I selected all styleranges within the document and i checked for the "[None]" style. If it was a match, i create a new characterStyle and add it to the corresponding text. This is my code:

       

      var styleRanges:Array = InDesign.app.activeDocument.stories.everyItem().textStyleRanges.everyItem().getElements();                     for (var i:uint = 0; i < styleRanges.length; i++) {                              if (styleRanges[i].appliedCharacterStyle.name == "[None]") {                                       var overriddenCharacterStyle:CharacterStyle = InDesign.app.activeDocument.characterStyles.add();                                       overriddenCharacterStyle.basedOn = TextStyleRange(styleRanges[i]).texts.item(0).appliedCharacterStyle;                                       overriddenCharacterStyle.name = String("customStyle"+i);                                                        TextStyleRange(styleRanges[i]).texts.item(0).applyCharacterStyle(overriddenCharacterStyle);                             }                     }

      This works well and all the text in the document now has its own characterStyle. But the problem is that al those custom characterStyles are empty. If one of the texts had a fontsize or a fontweight or something else applied to it, then it isnt passed to the characterStyle. Does anyone know what i'm doing wrong? How can i assign text to a characterStyle and pass all the style to that characterStyle to?

        • 1. Re: Creating a new characterStyle from text
          Peter Kahrel Adobe Community Professional & MVP

          I don't think you're doing anything wrong, it's just that you don't do something, namely, pass on the text properties to the style. Interactively, if you create a character style with some text selected, that text's properties are passed on to the character style. But in scripting it doesn't work that way: you need to pass on the properties yourself.

           

          Peter

          1 person found this helpful
          • 2. Re: Creating a new characterStyle from text
            vincent 1982 Level 1

            Hmm, when i try it like you say, i still get an error.

            var styleRanges:Array = InDesign.app.activeDocument.stories.everyItem().textStyleRanges.everyItem().getElements();       for (var i:uint = 0; i < styleRanges.length; i++) {              if (styleRanges[i].appliedCharacterStyle.name == "[None]") {                            var textProps:Object = styleRanges[i].texts.item(0).properties;                          var overriddenCharacterStyle:CharacterStyle = InDesign.app.activeDocument.characterStyles.add(textProps);                           overriddenCharacterStyle.basedOn =TextStyleRange(styleRanges[i]).texts.item(0).appliedCharacterStyle;                         overriddenCharacterStyle.name = String("customStyle"+i);                                                                                TextStyleRange(styleRanges[i]).texts.item(0).applyCharacterStyle(overriddenCharacterStyle);           }        }

            But apperently the properties allways gives the following error: "Error: In wrapReturnedHostObject(), attempted invalid wrap: com.adobe.indesign::Enumerator returned as Object"

            • 3. Re: Creating a new characterStyle from text
              Peter Kahrel Adobe Community Professional & MVP

              It works in JavaScript. Maybe you should ask in the SDK forum.

               

              Peter

              • 4. Re: Creating a new characterStyle from text
                vincent 1982 Level 1

                It seems to be a bug in the CSAWLib. See: http://forums.adobe.com/message/2871101#2871101

                 

                Does anyone know ways to bypass this? Or other methods to achieve the same result?

                • 5. Re: Creating a new characterStyle from text
                  Vamitul Level 4

                  it's not a bug, one of the differences of working in AS. either go through each (or at least the most important ones) of the properties and assign them to the new style (easy and simple, but a bit more typing involved) or find a way to pass the properties form one object to another (i'm thinking mabye JSON?)

                  • 6. Re: Creating a new characterStyle from text
                    vincent 1982 Level 1

                    Looping over all properties isnt very ideal with large documents. Are that really the only options i have?

                    • 7. Re: Creating a new characterStyle from text
                      Vamitul Level 4

                      uh.. here is how i'm thinking it (sorry, my AS3 is.. rusty to say the least).

                      convert the character style properties to a JSON. You only need to do this once. Use it as a template.

                      convert the text object's properties to a JSON. Use the character style properties template form the previous step to trim this JSON and only keep the needed properties (a simple function, some string manipulation involved).

                      use the trimmed JSON to pass the properties to the new character style.

                       

                      optional steps:

                      repeat the previous for paragraph styles

                      create a class so you can reuse for different projects

                      do feel free to share the said class with the lazy people like me who keep saying that they'll switch to AS soon