8 Replies Latest reply on Aug 17, 2013 11:21 AM by TᴀW

    Script for switching master pages

    Clark Kenyon Level 1

      Would it be possible to write a script to do this: Replace one master page with another whenever a certain paragraph style appears? I'm looking for a quick way to replace my A Master (with running header and page number) with the None Master on pages where new chapters begin.

      Clark

        • 1. Re: Script for switching master pages
          Peter Spier Most Valuable Participant (Moderator)

          Moved to the Scripting Forum...

          • 2. Re: Script for switching master pages
            Vamitul Level 4

            here you go (it also creates sections)

             

            var ProgressBar = function(/*str*/title){
                 var w = new Window('palette', ' '+title, {x:0, y:0, width:340, height:60}),
                      pb = w.add('progressbar', {x:20, y:12, width:300, height:12}, 0, 100),
                      st = w.add('statictext', {x:10, y:36, width:320, height:20}, '');
                 st.justify = 'center';
                 w.center();
                 this.reset = function(msg,maxValue)
                      {
                      st.text = msg;
                      pb.value = 0;
                      pb.maxvalue = maxValue||0;
                      pb.visible = !!maxValue;
                      w.show();
                      };
                 this.hit = function() {++pb.value;};
                 this.hide = function() {w.hide();};
                 this.close = function() {w.close();};
            };
            
            
            function main(){
            var doc = app.documents.item(0);
            
            
            // Create a list of paragraph styles
            var list_of_paragraph_styles = [];
            var all_paragraph_styles = [];
            var list_of_masters_names=[];
            var list_of_masters=[];
            
            
            for (var i= 0; i<doc.masterSpreads.length;i++){
                list_of_masters_names.push(doc.masterSpreads[i].name);
                list_of_masters.push(doc.masterSpreads[i]);
                };
            list_of_masters_names.push("undefined");
            doc.paragraphStyles.everyItem().name;
              
             for(var i = 0; i < doc.paragraphStyles.length; i++) {
                       list_of_paragraph_styles.push(doc.paragraphStyles[i].name);
                       all_paragraph_styles.push(doc.paragraphStyles[i]);
             }
            
            
            
            
            for(var i = 0; i < doc.paragraphStyleGroups.length; i++) {
                      for(b = 0; b < doc.paragraphStyleGroups[i].paragraphStyles.length; b++) {
                                list_of_paragraph_styles.push(doc.paragraphStyleGroups[i].name+"->"+doc.paragraphStyleGroups[i].paragraphStyles[b].name);
                                all_paragraph_styles.push(doc.paragraphStyleGroups[i].paragraphStyles[b]);
                      }
            }
            
            
            // Make the dialog box for selecting the paragraph styles
            var the_dialog = app.dialogs.add({name:"Section start at paragraph style:"});
            with(the_dialog.dialogColumns.add()){
                      with(dialogRows.add()){
                                staticTexts.add({staticLabel:"Find:"});
                      }
                      with(borderPanels.add()){
                                var find_first_paragraph = dropdowns.add({stringList:list_of_paragraph_styles, selectedIndex:0});
                      }
            with(dialogRows.add()){
                                staticTexts.add({staticLabel:"Apply Master:"});
                      }
                      with(borderPanels.add()){
                                var change_master = dropdowns.add({stringList:list_of_masters_names, selectedIndex:0});
                                staticTexts.add({staticLabel:"Select \"undefined\" not to change"});
                                //
                      }
            
            
            }
            the_dialog.show();
            
            
            // Define paragraph styles
            var myParagraph = all_paragraph_styles[find_first_paragraph.selectedIndex];
            var myMaster=(list_of_masters_names[change_master.selectedIndex]!="undefined")?list_of_masters[change_master.selectedIndex]:null;
            
            
            app.findChangeGrepOptions.includeFootnotes = false;
            app.findChangeGrepOptions.includeHiddenLayers = false;
            app.findChangeGrepOptions.includeLockedLayersForFind = false;
            app.findChangeGrepOptions.includeLockedStoriesForFind = false;
            app.findChangeGrepOptions.includeMasterPages = false;
            
            
            app.findGrepPreferences = NothingEnum.nothing;
            app.findGrepPreferences.appliedParagraphStyle = myParagraph;
            var myFound=doc.findGrep();
            
            
            var i=myFound.length;
            var pBar = new ProgressBar("Sectioning");
                 pBar.reset("Rabdare...", i);
            while (i--)
                {
                    pBar.hit();
                    if (myFound[i].parentTextFrames[0].parent.constructor.name=="Character"){
                        var myPage=myFound[i].parentTextFrames[0].parent.parentTextFrames[0].parentPage;}
                    else{
                 var myPage=myFound[i].parentTextFrames[0].parentPage;}
                 var mySection;
                 if (myPage.appliedSection.pageStart==myPage){mySection=myPage.appliedSection;}
                 else{
                 doc.sections.add(myPage,{pageNumberStart:1})};
                // mySection.pageStart=myPage;
                 //mySection.pageNumberStart=1;
                 if (myMaster) {myPage.appliedMaster=myMaster};
                }
            alert("Acum e gata!!");
            }
            app.doScript('main()',undefined,undefined,UndoModes.entireScript,'AutoSections');
            //main();
            
            
            • 3. Re: Script for switching master pages
              Laubender Adobe Community Professional & MVP

              @Vamitul – one option is missing.
              The "[None]" master spread (the spread with no name and no other properties):

               

              myPage.appliedMaster = null;
              

               

              Uwe

              • 4. Re: Script for switching master pages
                Vamitul Level 4

                huh.. yeah.... i guess you are right. never noticed (it's an ancient script i did for our workflow), since we never use [none]. wierd part is i always assumed that document.masterPages[0]==[none].

                 

                there are other bugs too, for example the cancel button does not work. I keep saying i will update it when i get the time, but so far time is something i keep lacking.

                • 5. Re: Script for switching master pages
                  [Jongware] Most Valuable Participant

                  (OT) "Acum e gata!" = "Look, a cat!" ? :-?

                  • 6. Re: Script for switching master pages
                    Vamitul Level 4

                    huh.. o.. what.. what cat?

                    ("Acum e gata!" - romainan- means "Now it's done!", and "Rabdare..."="Have patience..." )

                    • 7. Re: Script for switching master pages
                      Clark Kenyon Level 1

                      I appreciate the help, but this script is not working.

                      • 8. Re: Script for switching master pages
                        TᴀW Adobe Community Professional & MVP

                        Hi Clark,

                         

                        Check out my "Apply Masters based on Paragraph Styles" script, on

                         

                        www.freelancebookdesign.com

                         

                        under the scripting tab.

                         

                        You can download a demo there too.

                         

                        Ariel