0 Replies Latest reply: Apr 12, 2012 9:32 AM by Josh000000 RSS

    Auto master page application

    Josh000000

      I wrote this script to see if I could hook master pages to a paragraph style and I have a couple of questions.

       

      1. Currently I'm using the "onIdle" event type is there a better event type that could be used that would still run without interrupting the user?

       

      I was thinking the "afterAttributeChanged" event type might work better. When is this one called? Is it called for every attribute change in the layout window?

       

       

      2. I'm also searching for the pages that I want to change via grep and grabbing the page with paragraphs[i].parentTextFrames[0].parentPage;

       

      Is this the most efficient way to do this?

       

       

      Here's what I have so far:

       

      #targetengine "session"
      
      var doc = app.activeDocument;
      
      var chapterStyleGroup = doc.paragraphStyleGroups.itemByName("Chapter");
      var chapterTitle = chapterStyleGroup.paragraphStyles.itemByName("ChapterTitle");
      
      var aMaster = doc.masterSpreads.itemByName("A-Master");
      var cMaster = doc.masterSpreads.itemByName("C-Chapter Open");
      
      var chapterPages = [];
      
      app.findGrepPreferences = NothingEnum.nothing;
      app.findGrepPreferences.appliedParagraphStyle = chapterTitle;
      var paras = app.findGrep();
      for (var i=0, l=paras.length; i<l; i++) {
                var parentPage = paras[i].parentTextFrames[0].parentPage;
                chapterPages.push(parentPage);
                parentPage.appliedMaster = cMaster;
      };
      
      var listenerTask = app.idleTasks.add({sleep: 5000});
      listenerTask.addEventListener("onIdle", checkPages);
      
      function checkPages() {
                var savedPrefs = app.findGrepPreferences.properties;
                app.findGrepPreferences = NothingEnum.nothing;
                app.findGrepPreferences.appliedParagraphStyle = chapterTitle;
                var paras = app.findGrep();
      
                for (var i=0, l=paras.length; i<l; i++) {
                          var parentPage = paras[i].parentTextFrames[0].parentPage;
                          if (chapterPages[i] !== parentPage) {
                                    chapterPages[i].appliedMaster = aMaster;
                                    parentPage.appliedMaster = cMaster;
                                    chapterPages.splice(i,1,parentPage);
                          }
                };
                app.findGrepPreferences.properties = savedPrefs;
      }