Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Script to apply master page based on style sheet

Return to Member Forum

  • Author
    Posts
    • #82801
      Digby James
      Participant

      Does such a thing exist? I work on books from 100 to 700 pages and 5 to 50 chapters. I have a master page just for chapter starts, and the chapter title always has the same style sheet name. It would be nice if there was a script that would look through a document, find the chapter title, based on the style sheet, and then apply the chapter start master page. I’m a novice at scripting. I’ve done a little AppleScripting, but I probably need to learn JavaScript. But it would take me months. If someone already has one that would be great.

    • #82802
      Ari Singer
      Member

      I’m 99% sure that there’s a plug-in for that, but it costs money. I don’t remember the name at the moment, but I’ll try to look it up for you and let you know.

    • #82804
      David Blatner
      Keymaster

      Maybe this one?

      Mastermatic

      I think http:/In-Tools.com might also have one?

    • #82805
      Ari Singer
      Member

      According this old posting on the site (https://creativepro.com/pr-in-tools-releases-autoflow-pro-plug-in.php), in-tools “AutoFlow Pro can link paragraph styles to master pages and automatically apply the correct master page as necessary.”.

    • #83274
      Digby James
      Participant

      Thank you all for the helpful comments and links. I’ve been away and now ill, so please don’t think I was unappreciative.

      I’ve checked out Mastermatic and UnTools. InTools turned my 175 page document into a 2044 page one. Probably needs a more thorough reading of the manual.

      Mastermatic was more intuitive and after two attempts do exactly what I wanted. In the longer term I might invest in the InTools Publisher Suite as it offers lots of functions.

      Thanks again.

    • #83227
      Digby James
      Participant

      Thanks for the information and apologies for the delay in replying. Rushing around and illness are to blame.

      I’ve tried both. Mastermatic gave me a bizarre layout at first, but then reading more carefully it worked perfectly.

      AutoFlow Pro promises to be more powerful, but I need to read up a lot more. It gave me 2044 pages from a 160 page book, so I’m clearly doing something wrong.

      But the learning curve and cost of Mastermatic is so much less I’ll be buying that.

      Many thanks.

    • #92775
      Digby James
      Participant

      As a follow up, I’ve been watching the videos by Keith Gilbert on scripting InDesign on Lynda.com and have got the following to work:

      // Simple script to apply master page based on style sheet
      // My chapters always start with a heading set to “Heading 0 Chapter”
      // The applied master page for a chapter start is always “CSt-Chapter Start”
      // This master page is also applied to the verso if it is blank

      // Contents of script cribbed from various here: https://forums.adobe.com/thread/1562081

      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 == “Heading 0 Chapter”)
      {
      //Works only in CS5 and later
      myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item(“CSt-Chapter Start”);
      //for CS4 use this
      //myParas[i].parentTextFrames[0].parent.appliedMaster = myDocument.masterSpreads.item(“B-Master”);
      }
      }

      Now I need to get a script to apply the same master page to a blank page. I thought this would work:

      // This portion supposed to search and find pages with no text
      // Modified from Peter Kahrel’s script to find and delete blank pages found at
      // https://creativepro.com/topic/script-for-identifying-blank-pages-with-master-page-applied

      // This portion does not work properly as it also applies the master page to every master page and falls over
      // when trying to apply it to itself, so I need to find a way to exclude master pages.

      var myStories = app.activeDocument.stories.everyItem().getElements();
      for (i = myStories.length – 1; i >= 0; i–){
      var myTextFrames = myStories[i].textContainers;
      for (j = myTextFrames.length – 1; j >= 0; j–) {
      if (myTextFrames[j].contents == “”){
      myTextFrames[j].parentPage.appliedMaster = myDocument.masterSpreads.item(“CSt-Chapter Start”);
      }
      }
      }

      but as the comment says, it starts applying the master page to all the master pages, until it breaks when trying to apply the master page to itself. There must be some way of excluding master pages from this, but after a lot of hunting I’ve failed, thus far, to find the solution. I’m sure it’s very simple. Perhaps someone could point me in the right direction?

    • #92824

      What is wrong with Peters version?

      (function () {
      
      var curDoc = app.activeDocument;
      // reset everything to 'A-Master'
      curDoc.pages.everyItem().appliedMaster = curDoc.masterSpreads.itemByName("A-Master");
      
      var allParas = curDoc.stories.everyItem().paragraphs.everyItem().getElements();
      var nParas = allParas.length;
      
      for (var i = 0; i < nParas; i++) {
        var curPara = allParas[i];
        if (curPara.appliedParagraphStyle.name == "h1") {
          var parPage = curPara.parentTextFrames[0].parentPage;
          parPage.appliedMaster = curDoc.masterSpreads.itemByName("B-Master");
        }
      }
      
      var allPages = curDoc.pages.everyItem().getElements();
      var nPages = allPages.length;
      
      for (i = 0; i < nPages; i++) {
        var curPage = allPages[i];
        // check, if the page is either empty or the first text frame has no contents
        if (curPage.pageItems.length == 0 || curPage.textFrames[0].contents == "") {
          curPage.appliedMaster = curDoc.masterSpreads.itemByName("B-Master");
        }
      }
      
      }());
      
    • #92827
      Digby James
      Participant

      I don’t know what’s wrong with Peter’s version. But I do know that changing the master page name to the one I use and it works brilliantly. That’s just what I wanted.

      I’ll now study it with greater care to try and get my head round the niceties of Javascript.

      Thank you.

    • #99307
      Zalmen New
      Member

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

Viewing 9 reply threads
  • You must be logged in to reply to this topic.
Forum Ads