12 Replies Latest reply on Jun 29, 2012 11:37 AM by [Jongware]

    Apply Master Page using Paragraph Style

    Learner X Level 1

      Hi all,

       

      I am a beginner in script. Learning script through cs3 scripting tutorials.

       

      Depending upon the paragraph style it automatically apply master page.

       

      For example:

       

      Paragraph Style = "CT";      Want to Apply Master Page = "A-Master"

      Paragraph Style = "PT";       Want to Apply Master Page = "B-Master"

       

       

      These are the lines i am work out:

       

      var myDocument = app.activeDocument;

       

      var myPStyles = myDocument.paragraphStyles.everyItem().getElements();

      //var myPage = myDocument.pages.everyItem().getElements();

       

      for(i=0; i<myPStyles.length; i++)

      {

          if(myPStyles[i].appliedParagraphStyle == "CT")

          {

              myPStyles[i].appliedMaster = "A-Master";

              }

          }

       

       

      Where i did  mistake. Can anyone help me to resolve this problem.

       

      Thanks in advance genius...

        • 1. Re: Apply Master Page using Paragraph Style
          shankarInnovtech

          Hi

          I am shankar From Innov Technologies.

           

          plz check the paragraph name

           

          if(myPStyles[i].appliedParagraphStyle.name== "CT")

           

          {

          mydoc.pages.item(i).appliedMaster = mydoc.masterSpreads.item("B-Master");

          }

           

          I think this is Woking

           

          Enjoy it..

           

          Thanks

          • 2. Re: Apply Master Page using Paragraph Style
            Learner X Level 1

            Hi Shankar/All,

             

            Thanks for your reply.

             

            But this line again shows error in "appliedParagraphStyle.name".

             

             

             

            This is my latest lines.

             

            var myDocument = app.activeDocument;

             

            var myPStyles = myDocument.paragraphStyles.everyItem().getElements();

            var myPage = myDocument.pages.everyItem().getElements();

             

            for(i=0; i<myPStyles.length; i++)

            {

                if(myPStyles[i].appliedParagraphStyle.name == "CT")

                {

                    myPage[i].appliedMaster = myDocument.masterSpreads.item("B-Master");

                    }

                }

             

             

             

            Can anyone help for my script. Still facing error.

             

             

            Screen Shot 2012-06-22 at 9.19.22 PM.png

            • 3. Re: Apply Master Page using Paragraph Style
              [Jongware] Most Valuable Participant

              You are gathering a list of your document's paragraph styles, and those do not *have* an "appliedParagraphStyle" (because they *are* a style).

               

              I assume you want to inspect the actual style applied to paragraphs instead.

              • 4. Re: Apply Master Page using Paragraph Style
                shankarInnovtech Level 1

                var  mydoc = app.activeDocument;

                var myparastyles = mydoc.paragraphStyles.everyItem ().getElements();

                var pagelength = mydoc.pages.length;

                    for(i = 0; i < pagelength; i++)

                        {

                            var paras = mydoc.pages.item(i).textFrames.item(0).paragraphs.everyItem().getElements();

                            var paralength = paras.length;

                                for(j = 0; j < paralength; j++)

                                    {

                                        if(paras[j].appliedParagraphStyle.name == "H1")

                                            {

                                               mydoc.pages.item(i).appliedMaster = mydoc.masterSpreads.item("B-Master");

                                             }

                                    }

                        }

                • 5. Re: Apply Master Page using Paragraph Style
                  Learner X Level 1

                  Hi Jongware/Shankar/All,

                   

                  I tried below lines. Its working but not accurate. Please go through my below lines and give the solution.

                   

                  Can anyone give the solution?

                   

                   

                   

                  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")

                          {

                              myDocument.pages.item(i).appliedMaster = myDocument.masterSpreads.item("B-Master");

                              }

                          }

                  • 6. Re: Apply Master Page using Paragraph Style
                    [Jongware] Most Valuable Participant

                    > Leaner X wrote:

                    > Its working but not accurate.

                     

                    What does that mean? Just telling us "it does not work" tells us nothing.

                    • 7. Re: Apply Master Page using Paragraph Style
                      Green4ever Level 3

                      Try This one,

                       

                      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");
                              }
                          }
                      

                       

                      Good luck....

                      • 8. Re: Apply Master Page using Paragraph Style
                        banoo-LW

                        Try this. Its simple and also working

                         

                        myDoc=app.documents[0]

                        myPages=myDoc.pages.everyItem().getElements()

                         

                         

                        for(i=0; i<myDoc.pages.length; i++)

                        {

                                  for (j=0;j<myPages[i].textFrames[0].paragraphs.length; j++)

                                  {

                                            if(myDoc.pages[i].textFrames[0].paragraphs[j].appliedParagraphStyle.name=="CN")

                                            {

                                            myPages[i].appliedMaster=myDoc.masterSpreads.item("F-CN")

                                            }

                        }

                        }

                        alert("Master Page applied for all CN pages")

                        • 9. Re: Apply Master Page using Paragraph Style
                          Learner X Level 1

                          Hi Banoo_LW/All,

                           

                          Thanks for your reply....

                           

                          Can you check it at your end?

                           

                          Because while running the above script it shows the attached error.

                           

                          Can you clear and update it again.

                           

                          Thanks again....

                           

                           

                          Screen Shot 2012-06-29 at 7.08.14 PM.png

                           

                           

                          Thanks

                          Learner_X

                          • 10. Re: Apply Master Page using Paragraph Style
                            [Jongware] Most Valuable Participant

                            One of your pages may not have any text frames at all. The script expects one, and only one (*) text frame on each page.

                             

                            (*) So it might also be noted it won't work as expected on a page with more than one text frame, but that's a different issue, and it will not cause an error.

                            • 11. Re: Apply Master Page using Paragraph Style
                              [Jongware] Most Valuable Participant

                              See the scripts in this 2-year-old thread for some more ideas: http://forums.adobe.com/thread/593833

                              • 12. Re: Apply Master Page using Paragraph Style
                                [Jongware] Most Valuable Participant

                                I started from scratch, but the end result looks a lot like Green4ever's suggestion. (Which you by the way marked as "Correct" answer, only to continue with Banoo-LW's attempt.)

                                 

                                Note that (1) I tested with CS4, (2) you have to enter your own names for the paragraph style and master spread name, and (3) my way of getting to the page from a paragraph only works if you do not have text frames with your style on the pasteboard or in anchored text frames. (Strange -- it's okay to have it inside a table, but then it's ignored altogether.)

                                 

                                var pars = app.activeDocument.stories.everyItem().paragraphs.everyItem().getElements();
                                
                                for (i=0; i<pars.length; i++)
                                          if (pars[i].appliedParagraphStyle.name == "head2")
                                                    pars[i].parentTextFrames[0].parent.appliedMaster = app.activeDocument.masterSpreads.item("B-Master");