1 Reply Latest reply on Jun 8, 2018 4:41 AM by manuelb27477138

    Apply Master Pages to specific Group Styles and Paragraph Styles with ONE or TWO dimension names

    manuelb27477138 Level 1

      Hello!

      I have paragraph styles with names in ONE and TWO dimensions, and I would apply automatically Master Pages based on their paragraph styles.

      The number of the paragraph styles and master pages can increase and decrease, according to your needs.

       

      My styles ONE dimension paragraph Styles are:

      Chapter 1

      Chapter 2

      Chapter 3

      ...

       

      My styles TWO dimension paragraph Styles are:

      Chapter 1.1

      Chapter 1.2

      Chapter 1.3

      Chapter 2.1

      Chapter 2.2

      Chapter 2.3

      Chapter 3.1

      Chapter 3.2

      Chapter 3.3

      ...

       

      My Master pages are:

      C1-Chapter

      C2-Chapter

      C3-Chapter

      ....

        • 1. Re: Apply Master Pages to specific Group Styles and Paragraph Styles with ONE or TWO dimension names
          manuelb27477138 Level 1

          Here is the solutions:

           

          Here you can get the files, fotos, and code:

           

           

           

          SOLUTION FOR ONE DIMENSION PARAGRAPH

          //_____________________________________________________________________________________
          // COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME             
          
          
          var myDoc = app.documents[0];
               
          var paragraphStyle1 = [];     
          var counter = 0;     
          var paragraphStyleGroup1 = myDoc.paragraphStyleGroups.itemByName("CHAPTERS");  
          while(counter < paragraphStyleGroup1.paragraphStyles.length){      
              if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Chapter ") != -1){    
          paragraphStyle1.push(paragraphStyleGroup1.paragraphStyles[counter]);}      
            counter++;}   
          totalParagraphStyle1=paragraphStyle1.length;   
          alert("Total Paragraph Styles: " + totalParagraphStyle1);  
          
          
          //____________________________________________________________________________________
          //      END COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME
          
          
          
          
          
          
          
          
          
          
          //_____________________________________________________________________________________
          //                   START Apply Master Page based on Paragraph Style 
          
          
          var myDocument = app.activeDocument;
          var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
          var myPage = myDocument.pages;
          totalParagraphStyle1++;
          
          
          for(i=0; i<myParas.length; i++){
          
          
          for(j=0;j<totalParagraphStyle1;j++){
          if(myParas[i].appliedParagraphStyle.name == "Chapter "+[j]){
          myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("C"+[j]+"-Chapter");}}
          
          
          }
          alert("Done!");
          //_____________________________________________________________________________________
          //                     END Apply Master Page based on Paragraph Style 
          
          

          SOLUTION FOR TWO DIMENSION PARAGRAPH

           

          //_____________________________________________________________________________________
          // COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME    
          
          
          var myName = "CHAPTERS"
          var myArr =[];
          var myParaStyleList = myGetParagraphStyleNames();
          for (var i=0; i< myParaStyleList.length; i++)
          {
              if(myParaStyleList[i].indexOf(myName) !=-1)
              {
                  myArr.push(myParaStyleList[i]);
              }
          }
          alert("Total Paragraph Styles FIRST and SECOND dimension: " + myArr.length); // TOTAL OF PARAGRAPH STYLES
          var myArr1=[], myArr2 = [];
          for(var i =0; i< myArr.length; i++)
          {
              myArr1[i]=myArr[i].split(".")[0];
          }
          for(var i =0; i< myArr.length; i++)
          {
              myArr2[i]=myArr[i].split(".")[1];
          }
          myArr3= Unique(myArr1);
          alert("Total Paragraph Styles FIRST dimension: " + myArr3.length);   // TOTAL OF FIRST DIMENSION
          
          
          myArr4 = Unique(myArr2);
          alert("Total Paragraph Styles SECOND dimension: " + myArr4.length);   // TOTAL OF SECOND DIMENSION
          
          
          
          
          function myGetParagraphStyleNames()
          {
          var result = [];    
          var myParagraphStyles = app.activeDocument.allParagraphStyles;
          var myParagraphStyleName, obj;
          for(var i=0; i < myParagraphStyles.length ; i++)
          {
              myParagraphStyleName = myParagraphStyles[i].name;
              obj = myParagraphStyles[i];
              while(obj.parent instanceof ParagraphStyleGroup)
              {
                  myParagraphStyleName = obj.parent.name + ":" + myParagraphStyleName;
                  obj = obj.parent;        
              }
              result.push(myParagraphStyleName);
           } // for
          result.shift(); // удаление стиля No Paragraph Style
          return result;
          } // fnc
          
          
          function Unique(A)
          {
              var n = A.length, k = 0, B = [];
              for (var i = 0; i < n; i++) 
               { var j = 0;
                 while (j < k && B[j] !== A[i]) j++;
                 if (j == k) B[k++] = A[i];
               }
              return B;
          }
          
          
          //_____________________________________________________________________________________
          //                     END Apply Master Page based on Paragraph Style 
          
          
          
          
          
          
          
          
          
          
          //_____________________________________________________________________________________
          //                   START Apply Master Page based on Paragraph Style 
          
          
          var myDocument = app.activeDocument;
          var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
          var myPage = myDocument.pages;
          myArr3.length++;
          myArr4.length++;
          
          
          for(i=0; i<myParas.length; i++){
          for(AppendixA=0;AppendixA<myArr3.length;AppendixA++){
          for(AppendixB=0;AppendixB<myArr4.length;AppendixB++){
          if(myParas[i].appliedParagraphStyle.name == "Chapter "+[AppendixA]+"."+[AppendixB]){
          myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("C"+[AppendixA]+"-Chapter");}}}
          
          
          }
          
          
          //_____________________________________________________________________________________
          //                     END Apply Master Page based on Paragraph Style 
          
          
          alert("Done!");
          

           

           

           

           

           

           

           

          QUICK VIEW

          01.png

          02.png

           

           

          03.png

           

          04.png

           

          06.png

           

          06.png

           

          07.png

           

          08.png

          09.png

           

          10.png

           

          11.png

           

           

          12.png

           

          13.png

           

           

           

           

           

           

          14.png

           

           

          15.png

           

          17.png

           

           

           

           

           

           

           

           

           

           

           

          Thanks so much to Boris.