3 Replies Latest reply on Jan 30, 2015 10:02 AM by cchimi

    How to remove a basedOn style without losing formatting

    cchimi Level 2

      I have a problem that I've solved for my purposes, but I'm wondering if there's a cleaner solution. I have a function that removes the basedOn style from a paragraph or character style that is passed to it. I thought it was working, but it came to my attention that character styles were losing their formatting when their basedOn style was removed (actually, set to "[No Character Style]"). Setting the basedOn style to the "[None]" style has the same effect. I also tried creating a dummy None style and using that as my base, same effect. When you do this through InDesign's UI, the formatting is retained so I think it's a bug. My solution has been to create a new character style, copy all of the first style's properties (other than basedOn and properties) over to it, remove the original style (replacing it with the new style) and then rename the new style to match the old. As far as I can tell that works, but it seems like overkill. Does anyone have a better suggestion or hint?

       

      Here is the code that causes the problem:

      function unBaseStyle(funcSty){
          //Current style name.
          var myStyName = funcSty.name;
          //If the style is surrounded in brackets it is a default InDesign style and should not be unbased. In that case, skips to next style.
          if (myStyName.lastIndexOf("[", 0)  != -1) {return false;}
          else{
              var myBaseStyName = funcSty.basedOn.name;
              //Checks that the base style isn't undefined (which would indicate that it is already [No ... Style] and also that it isn't bracketed.
              if (myBaseStyName == undefined || myBaseStyName.lastIndexOf("[", 0)  != -1) {return false;}
              else{
                  //If the style is a paragraph style, bases it on [No Paragraph Style].
                  if (funcSty instanceof ParagraphStyle){funcSty.basedOn = "[No Paragraph Style]";}
                  //If the style is a character style, bases it on [No Character Style].
      //--->This is the line that causes the problem. funcDoc.characterStyles("None") has the same effect.
                  else{funcSty.basedOn = "[No Character Style]";}
                  return true;
              }
          }
      }
      

       

      Here is my working code:

      function unBaseStyle(funcDoc, funcSty){
          //Current style name.
          var myStyName = funcSty.name;
          //If the style is surrounded in brackets it is a default InDesign style and should not be unbased. In that case, skips to next style.
          if (myStyName.lastIndexOf("[", 0)  != -1) {return false;}
          else{
              var myBaseStyName = funcSty.basedOn.name;
              //Checks that the base style isn't undefined (which would indicate that it is already [No ... Style] and also that it isn't bracketed.
              if (myBaseStyName == undefined || myBaseStyName.lastIndexOf("[", 0)  != -1) {return false;}
              else{
                  //If the style is a paragraph style, bases it on [No Paragraph Style].
                  if (funcSty instanceof ParagraphStyle){funcSty.basedOn = "[No Paragraph Style]";}
                  //If the style is a character style, bases it on [No Character Style].
                      else{
                          var funcStyName = funcSty.name;
                          var newSty = funcDoc.characterStyles.add(funcStyName + " basedOnCopy");
                          var p
                          for (p in funcSty){
                              if (funcSty.hasOwnProperty(p) && p != "basedOn" && p != "properties"){
      //-->This copies over the properties; wrapped in try/catch because some properties are read-only.
                                  try{newSty[p] = funcSty[p];}catch(e){}}
                          }
                          funcSty.remove(newSty);
                          newSty.name = funcStyName;
                  return true;
              }
          }
      }
      

       

      Am I missing anything or is my solution more or less the way to go?