7 Replies Latest reply on May 26, 2014 5:29 PM by creativejoan0425

    Overide all master page's text frame only

    creativejoan0425 Level 1

      I have a document have 6 pages with 3 master page, A-master, B-master, C-master, A-master have 3 text frame, B-master is base on A-Master, and C-Master is base on B-Master

      P1-2 (Apply A-Master), P3-4 (Apply B-Master), P5-6(Apply C-Master)

       

      What I want to do is overrider the master page text Frame only, if my script is work, P1-6 will overrider the master text Frame, but it only overrider P1-2 text Frame, Please help to fix it! Many Thx

       

      var myDocument = app.activeDocument; 
      var TotalPages = (myDocument.pages.count()); 
      for(var CurrentPage=0; CurrentPage < TotalPages; CurrentPage++) { 
           OverrideMasterItems(); 

      function OverrideMasterItems() { 

      var allItems = myDocument.pages[CurrentPage].appliedMaster.pages.item(1).textFrames.everyItem().getEleme nts(); 

        for(var i=0;i<allItems.length;i++){ 

      try{

       

          allItems[i].override(myDocument.pages[CurrentPage])} 
      catch(e){} 

        } 

        var allItems0 = myDocument.pages[CurrentPage].appliedMaster.pages.item(0).textFrames.everyItem().getEleme nts(); 

        for(var i=0;i<allItems0.length;i++){ 

      try{

       

          allItems0[i].override(myDocument.pages[CurrentPage])} 
      catch(e){} 

        } 

       

       

       

      }

        • 1. Re: Overide all master page's text frame only
          csm_phil Level 4

          Hi creativejoan0425,

           

          Pleas try the below js code.

           

          var myDocument = app.activeDocument; 
          var TotalPages = (myDocument.pages); 
          for(var i=0; i< TotalPages.length; i++) { 
              if(TotalPages[i].appliedMaster.name=="A-Master"){
                  var allItems  = (TotalPages[i].appliedMaster.textFrames);
                  for(var a=allItems.length-1; a>=0; a--){ 
                      try{
                          allItems[a].override(TotalPages[i])} 
                      catch(e){} 
                      }
                  }
              } 
          
          

           

           

          thx,

          csm_phil

          • 2. Re: Re: Overide all master page's text frame only
            Jump_Over Level 5

            Hi,

             

            @csm_phil

            The problem starts when masterSpread has more that 1 page with textFrames placed there.

            Notice that page.appliedMaster is a spread (not a page), ==> so allItems collects more textFrames than you need.

            Some of allItems[a] can not be overriden on a specific page and results with "wrong object" error.


            @creativejoan0425

            I suggest below solution:

            var
              curDoc = app.activeDocument,
              mPages = curDoc.pages.everyItem().getElements(),
              cnt = mPages.length,
              mFrames, len;
            while (cnt-->0) {
              mFrames = collectMasterFrames ( mPages[cnt] );
              len = mFrames.length;
              while (len-->0)
              mFrames[len].override(mPages[cnt]);
              }
            
            function collectMasterFrames (page) {
              var cMasterSpread = page.appliedMaster;
              if (cMasterSpread == null) return;
              var
                 cFactor = cMasterSpread.pages.count(),
                 cSpreadIdx = page.index % cFactor,
                 cFrames = cMasterSpread.pages[cSpreadIdx].textFrames.everyItem().getElements();
            
              if (cMasterSpread.pages[cSpreadIdx].appliedMaster != null)
                 cFrames = cFrames.concat( collectMasterFrames ( cMasterSpread.pages[cSpreadIdx] ) );
            
              return cFrames;
              }
            
            
            
            
            

             

            Jarek

            • 3. Re: Re: Overide all master page's text frame only
              creativejoan0425 Level 1

              I try your script if my page start at page 2 is ok if start at page 1 it will work (P2-P6) ,. and pop up a error message

               

              qqq.jpg

              • 4. Re: Overide all master page's text frame only
                Jump_Over Level 5

                Hi,

                Can you switch facingPages OFF?

                 

                Jarek

                • 5. Re: Overide all master page's text frame only
                  creativejoan0425 Level 1

                  no, coz sometimes need facing Pages sometime no need facing Pages, can make both work?

                  • 6. Re: Re: Overide all master page's text frame only
                    Jump_Over Level 5

                    Hi,

                     

                    OK. It looks much easier than I expected, since there is a page property masterPageItems

                     

                    So:

                    var
                      mDoc = app.activeDocument,
                      mPages = mDoc.pages.everyItem().getElements(),
                      cFrames, len,
                      cnt = mPages.length;
                    
                      while (cnt-->0) {
                           cFrames = mPages[cnt].masterPageItems;
                           len = cFrames.length;
                           while (len-->0)
                                if (cFrames[len].constructor.name == "TextFrame")
                                     cFrames[len].override(mPages[cnt]);
                           }
                    
                    

                     

                    Jarek