6 Replies Latest reply on May 23, 2016 11:45 PM by tpk1982

    Find Master Page A and apply Master Page B

    Obi-wan Kenobi Adobe Community Professional

      Hi,

       

      Here's what I've written:

       

      var myDoc = app.documents[0];    
      var myMasterSpreads = myDoc.masterSpreads;    
      var myPages = myDoc.pages(appliedMaster("A"));    
      for(n=0;n<myPages.length;n++){    
          myPages[n].appliedMaster = myMasterSpreads.itemByName("B");
          };
      

       

      Of course, it doesn't work [ it would be too easy for a beginner like me!  ]

       

      Thanks in advance for debugging it! 

        • 1. Re: Find Master Page A and apply Master Page B
          TᴀW Adobe Community Professional & MVP

          Close, but (unless there's some property that lists all pages that a master page is applied to, which I don't remember offhand), you'll have to loop as follows:

           

          var myDoc = app.documents[0];
          var myMasterSpreads = myDoc.masterSpreads;
          var theSpread = myMasterSpreads.itemByName("A");
          var myPages = myDoc.pages;
          for (n = 0; n < myPages.length; n++){
               if (myPages[n].appliedMaster == theSpread){
                    myPages[n].appliedMaster = myMasterSpreads.itemByName("B");
               };
          };
          

           

          I haven't tested this, but that would be the general approach...

           

          Ariel

          • 2. Re: Find Master Page A and apply Master Page B
            Obi-wan Kenobi Adobe Community Professional

            Cool! 

             

            Thanks a lot Ariel! It works fine!

             

            [ For comment: take a look at: http://www.id-extras.com/products/mastermatic

            … Mastermatic, written by Ariel (Walden), is a very great script that plays master pages "in live"! ]

            • 3. Re: Find Master Page A and apply Master Page B
              Obi-wan Kenobi Adobe Community Professional

              New deal!

               

              Now, I'm trying to add an UI to the script above (validated by Ariel).

               

              For that, I've modified a script written in 2009 by Jongware:

               

              var myDialog = app.dialogs.add ({name:"Search a para style AND apply a master page",canCancel:true});
              var allStyles = new Array;
              var styleRefs = new Array;
              var allMasters = new Array;
              var myDoc = app.documents[0];  
              var myMasterSpreads = myDoc.masterSpreads;
              
              
              pushParGroup ("", myDoc);
              
              
              for (i=0; i<myDoc.masterSpreads.length; i++)
                allMasters.push(myDoc.masterSpreads[i].name);
                  
              with (myDialog)
              {
                with (dialogColumns.add())
                {
                with (dialogRows.add())
                {
                staticTexts.add ({staticLabel:"Find this master page:"});
                dropMaster = dropdowns.add ({stringList:allMasters, selectedIndex:0});
                            var mySpread_1 = myMasterSpreads.item(allMasters[dropMaster.selectedIndex]);
                }
                with (dialogRows.add())
                {
                staticTexts.add ({staticLabel:"… AND apply this master page:"});
                dropMaster = dropdowns.add ({stringList:allMasters, selectedIndex:0});
                            var mySpread_2 = myMasterSpreads.item(allMasters[dropMaster.selectedIndex]);
                }
                }
              }
              
              
              if (myDialog.show() == true)
              {
                  var myPages = myDoc.pages;  
                  for (n = 0; n < myPages.length; n++){  
                      if (myPages[n].appliedMaster == mySpread_1){  
                          myPages[n].appliedMaster = myMasterSpreads.itemByName(mySpread_2);  
                      };  
                  };
              }
              
              
              function pushParGroup (namePrefix, styleSource)
              {
                for (var i=0; i<styleSource.paragraphStyles.length; i++)
                {
                allStyles.push(namePrefix+styleSource.paragraphStyles[i].name);
                styleRefs.push(styleSource.paragraphStyles[i]);
                }
                for (var i=0; i<styleSource.paragraphStyleGroups.length; i++)
                pushParGroup (namePrefix + styleSource.paragraphStyleGroups[i].name + "/", styleSource.paragraphStyleGroups[i]);
              }
              

               

              … But it doesn't work!

              • 4. Re: Find Master Page A and apply Master Page B
                Stefan Rakete Level 2

                I changed your code in some lines. This works for me:

                var myDialog = app.dialogs.add ({name:"Search a para style AND apply a master page",canCancel:true}); 
                var allStyles = new Array; 
                var styleRefs = new Array; 
                var allMasters = new Array; 
                var myDoc = app.documents[0];   
                var myMasterSpreads = myDoc.masterSpreads; 

                 

                pushParGroup ("", myDoc); 

                   

                for (i=0; i<myDoc.masterSpreads.length; i++) 
                  allMasters.push(myDoc.masterSpreads[i].name); 
                    
                with (myDialog) 
                  with (dialogColumns.add()) 
                 
                  with (dialogRows.add()) 
                 
                  staticTexts.add ({staticLabel:"Find this master page:"}); 
                  dropMaster1 = dropdowns.add ({stringList:allMasters, selectedIndex:0}); 
                             // mySpread_1 = myMasterSpreads.item(allMasters[dropMaster1.selectedIndex]); 
                 
                  with (dialogRows.add()) 
                 
                  staticTexts.add ({staticLabel:"… AND apply this master page:"}); 
                  dropMaster2 = dropdowns.add ({stringList:allMasters, selectedIndex:0}); 
                             // mySpread_2 = myMasterSpreads.item(allMasters[dropMaster2.selectedIndex]); 
                 
                 
                if (myDialog.show() == true) 
                    mySpread_1 = myMasterSpreads.item(allMasters[dropMaster1.selectedIndex]); 
                    mySpread_2 = myMasterSpreads.item(allMasters[dropMaster2.selectedIndex]); 
                    var myPages = myDoc.pages;   
                    for (n = 0; n < myPages.length; n++){
                        if (myPages[n].appliedMaster == mySpread_1) {   
                            myPages[n].appliedMaster = myMasterSpreads.itemByName(mySpread_2.name);   
                        }; 

                 

                        };   
                    }; 
                function pushParGroup (namePrefix, styleSource) 
                  for (var i=0; i<styleSource.paragraphStyles.length; i++) 
                 
                  allStyles.push(namePrefix+styleSource.paragraphStyles[i].name); 
                  styleRefs.push(styleSource.paragraphStyles[i]); 
                 
                  for (var i=0; i<styleSource.paragraphStyleGroups.length; i++) 
                  pushParGroup (namePrefix + styleSource.paragraphStyleGroups[i].name + "/", styleSource.paragraphStyleGroups[i]); 
                • 5. Re: Find Master Page A and apply Master Page B
                  Marc Autret Level 4

                  Hi Obi,

                   

                  What is not clear to me is the relationship you want to draw between paragraph style(s) and master replacement (?)

                   

                  Your code collect styles (cf allStyles) but these data don't seem to be used at all. What exactly do you need to achieve with those styles?

                   

                  @+

                  Marc

                  • 6. Re: Find Master Page A and apply Master Page B
                    tpk1982 Level 4

                    I am using this script for nearly 4 years.. to apply master page depend upon paragraph style

                     

                    var myDialog = app.dialogs.add ({name:"Styles to Master",canCancel:true});
                    var allStyles = new Array;
                    var styleRefs = new Array;
                    
                    
                    var allMasters = new Array;
                    
                    
                    pushParGroup ("", app.activeDocument);
                    
                    
                    
                    
                    for (i=0; i<app.activeDocument.masterSpreads.length; i++)
                      allMasters.push(app.activeDocument.masterSpreads[i].name);
                    
                    
                    with (myDialog)
                    {
                      with (dialogColumns.add())
                      {
                      with (dialogRows.add())
                      {
                      staticTexts.add ({staticLabel:"For style"});
                      dropStyle = dropdowns.add ({stringList:allStyles, selectedIndex:0});
                      }
                      with (dialogRows.add())
                      {
                      staticTexts.add ({staticLabel:"Apply Master"});
                      dropMaster = dropdowns.add ({stringList:allMasters, selectedIndex:0});
                      }
                      with (dialogRows.add())
                      {
                      makeBlank = checkboxControls.add ({staticLabel:"Make left page blank"});
                      }
                      }
                    }
                    
                    
                    if (myDialog.show() == true)
                    {
                      app.findTextPreferences = null;
                      app.findTextPreferences.appliedParagraphStyle = styleRefs[dropStyle.selectedIndex];
                    
                    
                      foundInstances = app.activeDocument.findText();
                      for (i=0; i<foundInstances.length; i++)
                      {
                      page = foundInstances[i].parentTextFrames[0].parent;
                    
                    
                      page.appliedMaster = app.activeDocument.masterSpreads.item(allMasters[dropMaster.selectedIndex]);
                      if (makeBlank.checkedState == true)
                      {
                      previousPage = app.activeDocument.pages.previousItem (page);
                      if (previousPage != null)
                      {
                      for (f=0; f<previousPage.textFrames.length; f++)
                      {
                      if (previousPage.textFrames[f].contents.length > 1)
                      break;
                      }
                      if (f == previousPage.textFrames.length)
                      previousPage.appliedMaster = null;
                      }
                      }
                      }
                    }
                    
                    
                    function pushParGroup (namePrefix, styleSource)
                    {
                      for (var i=0; i<styleSource.paragraphStyles.length; i++)
                      {
                      allStyles.push(namePrefix+styleSource.paragraphStyles[i].name);
                      styleRefs.push(styleSource.paragraphStyles[i]);
                      }
                      for (var i=0; i<styleSource.paragraphStyleGroups.length; i++)
                      pushParGroup (namePrefix + styleSource.paragraphStyleGroups[i].name + "/", styleSource.paragraphStyleGroups[i]);
                    }