4 Replies Latest reply on Jan 2, 2013 10:45 AM by Trevorׅ

    duplicate "[None]" characterstyle

    vincent 1982 Level 1



      I have a bit  text that has some format (fontsize, fontweight, color, etc) it doesnt have a characterstyle applied to it thou, so it hase the "[None]" style. I would like to duplicate that style and add a new name to it. But the "[None]" style doesnt like to be duplicated apparently, it gives me the following error:


      <code>Error: Invalid request on a root style.</code>


      the code i use is this:


      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 dupeCharStyle:CharacterStyle = styleRanges[i].appliedCharacterStyle.duplicate();

                          dupeCharStyle.name = String("vit2printstyle"+i);





      But as said, this doesnt work.

      What would be the best, and most importantly, the fastest, way to achieve this?

        • 1. Re: duplicate "[None]" characterstyle
          Trevorׅ Adobe Community Professional

          Hi Vincent,


          This is the way to go (with a very little bit of imagination for the changes you need to make)



          prop = app.selection[0].properties;
          cs = app.activeDocument.characterStyles.add();
          cs.properties = prop;
          • 2. Re: duplicate "[None]" characterstyle
            vincent 1982 Level 1

            Works perfectly, only thing i have some doubts about is that it adds all properties to the character style, is it possible to just add the styles that dont match the original ones? for example:

            for (var i = 0; i < styleRanges.length; i++) {                if (styleRanges[i].appliedCharacterStyle.name === "[None]") {                        prop = styleRanges[i].properties;                        cs = app.activeDocument.characterStyles.add();                                      cs.name = "charstyle" + i;                       cs.basedOn = styleRanges[i].appliedCharacterStyle;                        cs.properties = prop;                                     styleRanges[i].applyCharacterStyle(cs);                }      }

            now all properties get added to the characterstyle, but is it possible to add only the ones that don't match the properties of the style is is based on?

            • 3. Re: duplicate "[None]" characterstyle
              Trevorׅ Adobe Community Professional

              1) To format your code click on the >> next to the Smile then Syntax Highlighting then Java.


              2) I am confused by what you want to do and your code.


              What you seem to want to do is whenever you come accross  a styleRange that has not character style applied to it and therefore has the "[None]" Style applied to it to make a new style based on "None" with just with the relevant deviations from the "None" style.


              This is problematic because the "None" style has all it's values set to either 851876449 (NothingEnum.NOTHING) or null;

              Run bellow code for demo, with the JavaScript Console open.



              noneProperties = app.activeDocument.characterStyles.itemByName("[None]").properties;
              ms = app.selection[0];
              msp = ms.properties;
              for (n in noneProperties) $.writeln("[None]: "+noneProperties[n]+"\tSelect: "+msp[n]+ "\tProp: "+ n);
              // 1851876449 = NothingEnum.NOTHING


              One way to get around this is like this.  If I was getting paid I would try to optimize it a bit more.



              //script to create new character styles by Trevor http://forums.adobe.com/message/4961504#4961504
              redraw = app.scriptPreferences.enableRedraw;
              app.scriptPreferences.enableRedraw =0; // speeds up script
              app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Trevor's create new character styles");
              app.scriptPreferences.enableRedraw = redraw;
              function main()
                      var doc =  app.activeDocument,
                      tempFrame = doc.pages[-1].textFrames.add({geometricBounds:["-12mm",0,"0mm","50mm"], contents: "Hi Vincent"});
                      var tempNoneStyle = tempFrame.textStyleRanges[0].properties,
                      noneProperties = app.activeDocument.characterStyles.itemByName("[None]").properties,
                      myTextRanges = doc.stories.itemByRange (0,-2).textStyleRanges.everyItem().getElements();
                      var l = myTextRanges.length;
                      while (l--)  if (myTextRanges[l].appliedCharacterStyle.name == "[None]")            {
                              NONE = {};
                              myProps = myTextRanges[l].properties;
                              for (n in noneProperties) if (myProps[n] != tempNoneStyle[n]) NONE[n] = myProps[n];
                              NONE.basedOn = "[None]";
                              var cs = app.activeDocument.characterStyles.add();
                              cs.properties = NONE;
              • 4. Re: duplicate "[None]" characterstyle
                Trevorׅ Adobe Community Professional

                In the code I posted above a forgot the if statment after the while, I fixed it now with an edit.

                while (l--)  if (myTextRanges[l].appliedCharacterStyle.name == "[None]")