12 Replies Latest reply on Nov 6, 2017 3:44 AM by Laubender

    Script for Applying master Spread

    Math Kay Level 1

      Hi,

      This Script is for Applying CO-Opener master page  wherever CN is applied, When we run the script, it alerts Sucess.But master spread is not applied . Please resolve.

       

       

      var mydoc=app.activeDocument; 
      var mypara = mydoc.stories.item(0).paragraphs.everyItem().getElements();
      for(i=0; i<mypara.length; i++){
          if (myPara[i].appliedParagraphStyle.name=="CN"){
                 mypara[i].parentTextFrames[0].parent.appliedMaster = mydoc.masterSpreads.item("CO-Opener");
              }
          }
      alert("Master Page applied Sucessfully")
      
        • 1. Re: Script for Applying master Spread
          TᴀW Adobe Community Professional & MVP

          I wanted to mention that if you're looking for a complete solution to

          applying master pages based on paragraph styles (and/or object styles),

          check out my Mastermatic script:

           

          http://www.id-extras.com/products/mastermatic

           

          It even updates the master pages automatically while you work!

          • 2. Re: Re: Script for Applying master Spread
            BEGINNER_X Level 3

            Hi Math,

             

            I hope your coding is correct, but it seems stories[0] do not have "CN" Styles.

             

            Change the below line:

            var mypara = mydoc.stories.item(0).paragraphs.everyItem().getElements(); 


            into

            var mypara = mydoc.stories.everyItem().paragraphs.everyItem().getElements(); 


            Thanks to Jongware and Green4ever.....


            and also the use the below url to get the correct coding for CS4 and CS5 and above


            Apply Master Page using Paragraph Style


            var myDocument = app.activeDocument;  
            var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();  
            var myPage = myDocument.pages;  
            for(i=0; i<myParas.length; i++)  
            {  
                if(myParas[i].appliedParagraphStyle.name == "CT")  
                {  
                    //Works only in CS5 and later  
                    myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("B-Master");  
                    //for CS4 use this  
                    //myParas[i].parentTextFrames[0].parent.appliedMaster = myDocument.masterSpreads.item("B-Master");  
                    }  
                }  
            
            

            Regards

            Siraj





            • 3. Re: Script for Applying master Spread
              Math Kay Level 1

              Hi,

               

              When we change into stories.everyItem(), It works fine.

               

              Thank you.

              • 4. Re: Script for Applying master Spread
                chadb78135425 Level 1

                Could this script be easily modified to only apply to selected pages instead of throughout the document?

                I've found a lot of things that don't work.  #beginner

                • 5. Re: Script for Applying master Spread
                  Laubender Adobe Community Professional & MVP

                  Hi Chad,

                  not easily and definitely not easily for beginners.

                   

                  What do you mean by "selected pages"?
                  Do you want to provide a page range?
                  Or do you rely on selected pages in the Pages Panel?

                   

                  Regards,
                  Uwe

                  • 6. Re: Script for Applying master Spread
                    chadb78135425 Level 1

                    Hello Uwe,

                    I had in mind selected pages in the pages panel but a page range would work just as well.

                     

                    If it's to much trouble don't bother with it.

                     

                    Thanks,

                    Chad

                    • 7. Re: Script for Applying master Spread
                      chneora Level 1

                      there is a way to do the same script, but with Character style ?

                      • 8. Re: Script for Applying master Spread
                        Laubender Adobe Community Professional & MVP

                        chneora  wrote

                         

                        there is a way to do the same script, but with Character style ?

                        Hi,

                        basically, yes.

                        If you are working with textStyleRanges and check the appliedCharacterStyle.

                        Be aware that would not include used character styles for Nested Styles, Nested Line Styles, Nested Grep Styles etc.pp.

                         

                        What I do not understand is why the script of BEGINNER_X only considers the first parent text frame of a paragraph. There could be others as well. Basically a paragraph could be split between two (or more) pages and two (or more) text frames. Also: Text could be positioned outside the page on the pasteboard so parentPage would return null. Text could be on a master. Would you like to apply a master to a master? And finally: How about text with tables and footnotes? You will not catch that with stories.everyItem().

                         

                        For textStyleRanges see e.g.:

                        Adobe InDesign CS6 (8.0) Object Model JS: TextStyleRange

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

                         

                        Regards,

                        Uwe

                        • 9. Re: Script for Applying master Spread
                          chneora Level 1

                          Thanks

                           

                          How do i change to make IT WORK not only the first parent text frame of a paragraph, but all of them ?

                          • 10. Re: Script for Applying master Spread
                            Laubender Adobe Community Professional & MVP

                            Hi,

                            parentTextFrames is an array.

                            Loop that array.

                             

                            Regards,
                            Uwe

                            • 11. Re: Script for Applying master Spread
                              [Jongware] Most Valuable Participant

                              In that case it'd just be the text frames for that single paragraph. To loop over all text frames for all paragraphs, you need parentStory. But somehow I don't think that is the answer OP is looking for, because then you don't need that check for individual character styles.

                              • 12. Re: Script for Applying master Spread
                                Laubender Adobe Community Professional & MVP

                                Hi Jongware,

                                I answered to chneora's question that revolves around text where the applied character style should lead to apply a certain master. The OP was fine—I think—with BEGINNER_X's solution. And that works perfectly on the assumption that the first parent text frame of a found paragraph is fully on one single page.

                                 

                                Here some code where a distinct character style named "CharStyle1" will trigger a master named "B-Master".

                                Assumption is for instance that text on masters should be ignored, text on the pasteboard should be ignored and in the case that text is running between pages and is formatted with "CharStyle1" all pages where that formatting is running should be considered.

                                 

                                Basically I followed BEGINNER_X's solution.

                                This is just one way to handle it.

                                 

                                Maybe it's better to do a Text Find looking for that character style and exploit the results?

                                Could be way faster.

                                 

                                 

                                app.doScript
                                    (
                                   
                                    applyMasterToPage,
                                    ScriptLanguage.JAVASCRIPT,
                                    [],
                                    UndoModes.ENTIRE_SCRIPT,
                                    "Apply Master To Pages where Char Style is Applied"
                                   
                                    );
                                   
                                function applyMasterToPage()
                                {
                                
                                    // Works on the active document:
                                    var doc = app.documents[0];
                                
                                    // You need a character style with that name in the root of your character styles panel:
                                    var charStyle = doc.characterStyles.itemByName("CharStyle1");
                                
                                    // You need a master with that name in your document:
                                    var master = doc.masterSpreads.itemByName("B-Master");
                                
                                    // Collect all text style ranges of all stories.
                                    // Not included here are tables, footnotes!
                                    var docTextStyleRanges = app.documents[0].stories.everyItem().textStyleRanges.everyItem().getElements();
                                
                                    var textStyleRangesLength = docTextStyleRanges.length;
                                
                                    // Test the text style ranges and their text containers.
                                    // Note: Used character styles for Nested Styles, Nested Line Styles, Nested Grep Styles, bullets etc. are out of the scope here!
                                    // ONLY directly applied character styles are considered!
                                
                                    for(var n=0;n<textStyleRangesLength;n++)
                                    {
                                        if(docTextStyleRanges[n].appliedCharacterStyle != charStyle){ continue };
                                       
                                        var textContainers = docTextStyleRanges[n].parentTextFrames;
                                       
                                        for(var t =0;t<textContainers.length;t++)
                                        {
                                            if(textContainers[t].parentPage == null){continue};
                                            if(textContainers[t].parentPage.appliedMaster == master){continue};
                                            if(textContainers[t].parentPage.parent.constructor.name == "MasterSpread"){continue};
                                           
                                            textContainers[t].parentPage.appliedMaster  = master;
                                        };
                                
                                    }
                                
                                };
                                

                                 

                                Regards,
                                Uwe

                                1 person found this helpful