3 Replies Latest reply on Nov 30, 2018 12:19 AM by Johnwhite

    Change style basedOn

    Johnwhite Level 1

      Hi experts,

       

      How to change my paragraphs style basedOn?

       

      my script as below, but not working

       

      var doc = app.activeDocument;  
      var paraStyles = doc.allParagraphStyles;
      var basicParagraphStyle1 = doc.paragraphStyles("01 Body");
      var basicParagraphStyle2 = doc.paragraphStyles("06 Notes");
      var paraStyles = doc.allParagraphStyles;
              for(var n=0; n<paraStyles.length; n++)     
              {
                  if(paraStyles[n].name.match(/03|04|05|06|07|08|09|10|11|12|13|14|15|16/g)) {
                      paraStyles[n].basedOnStyle == basicParagraphStyle1;
                  } 
                  else if(paraStyles[n].name.match(/17|18|19|20|21|22|23|24|25|26|27|28|29|30/g)) {
                      paraStyles[n].basedOnStyle == basicParagraphStyle2;
                  } 
          }
      

       

      Could someone please give me some opinions please.

       

      Thanks

      Regard

      John

        • 1. Re: Change style basedOn
          Sudha K Level 1

          Hi,

           

               Please use the below code.

           

          var doc = app.activeDocument;  
          var paraStyles = doc.allParagraphStyles;
          var basicParagraphStyle1 = doc.paragraphStyles.item("01 Body");
          var basicParagraphStyle2 = doc.paragraphStyles.item("06 Notes");
          var paraStyles = doc.allParagraphStyles;
          for(var n=0; n<paraStyles.length; n++)
          {
          if(paraStyles[n].name.match(/03|04|05|06|07|08|09|10|11|12|13|14|15|16/g)) {
          paraStyles[n].basedOn = basicParagraphStyle1;
          else if(paraStyles[n].name.match(/17|18|19|20|21|22|23|24|25|26|27|28|29|30/g)) {
          paraStyles[n].basedOn = basicParagraphStyle2;
          }

           

           

               When assigning value to the variable should not use "==".  "==" should be used in condition case only.

          • 2. Re: Change style basedOn
            Manan Joshi Adobe Community Professional

            Hi John,

             

            You need to look at the object model for the name of the properties you are trying to set. A good place to start exploring objects and their methods/properties is

            https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

             

            Other older version of the documentation can be found at in

            https://www.indesignjs.de/extendscriptAPI/

             

            Now with regards to you code in addition to the rectifications done by Sudha K in your code i have some more points to share, look at comments in the code below

            var doc = app.activeDocument;   
            var paraStyles = doc.allParagraphStyles; 
            var basicParagraphStyle1 = doc.paragraphStyles.item("01 Body"); 
            var basicParagraphStyle2 = doc.paragraphStyles.item("06 Notes"); 
            
            //You need to test if the pstyles you got above are valid or not, what if these styles did not exist in the document
            if(!basicParagraphStyle1.isValid)
            {
                alert("Style 01 Body does not exist in the document")
                exit()
            }
            if(!basicParagraphStyle2.isValid)
            {
                alert("Style 06 Body does not exist in the document")
                exit()
            }
            var paraStyles = doc.allParagraphStyles;      //You don't need this line, this is done in line no 2, remove this
            for(var n=0; n<paraStyles.length; n++)     
            { 
                if(paraStyles[n].name.match(/03|04|05|06|07|08|09|10|11|12|13|14|15|16/g)) { 
                      paraStyles[n].basedOn = basicParagraphStyle1;    // Here you used == instead of =, in addition to the wrong property name
                } 
                else if(paraStyles[n].name.match(/17|18|19|20|21|22|23|24|25|26|27|28|29|30/g)) { 
                      paraStyles[n].basedOn = basicParagraphStyle2;  // Here you used == instead of =, in addition to the wrong property name
                } 
            }
            

            -Manan

            • 3. Re: Change style basedOn
              Johnwhite Level 1

              thanks so much Manan

               

              John