4 Replies Latest reply on Nov 14, 2016 11:33 PM by absolutely23

    Trying to filter a jpeg export by Master Page

    absolutely23

      This is an InDesign script to convert indd files into jpgs and then export and rename them into a folder on my desktop. It all works fine, but there is one part that I'm trying to do, which is only export the pages that do not have the Master Page "H-Advertising" applied. I've written an if statement that checks what master page has been applied to the current page of the current document and should ostensibly only export that page if it hasn't had "H-Advertising" applied as a master page. I know that the loop works if I add a different condition (such as if (3!=4)) and it is also able to alert the master page of each page, but it just seems to go ahead and add the page to the array of pages I want to export no matter what. Also, the appliedMaster variable has the correct value, i.e. even if it's "H-ADVERTISING" it still goes ahead and executes the code below

       

      Main();

        
      function Main() {
        
      // Check to see whether any InDesign documents are open.
       
      // If no documents are open, display an error message.
       
      if (app.documents.length > 0) {
        app
      .jpegExportPreferences.exportingSpread = false;

        
      //makes sure there is a book open
        
      if (app.books.length != 1)
        alert
      ("This only works when you have one (1) book open and the first file in that book open");
        
      else

        
      //loop through the book's stories
        
      for (b = 0; b < app.books[0].bookContents.length; b++) {
        
      // initialize pages variable
        
      var pages = [];
        
      // loop through the pages in the active document
        
      for (i = 0; i < app.activeDocument.pages.length; i++) {

        
      // initialize variable holding document name, and then rename as follows
        
      var myDocumentName = app.books[0].bookContents[b].fullName;
        c
      = app.open(app.books[0].bookContents[b].fullName);
        myDocumentName
      = myDocumentName.name.replace("indd", "jpg");
        myDocumentName
      = myDocumentName.replace("WN16", "WN16_");

        
      // get value of the current page's applied master
        
      if (app.activeDocument.pages[i].appliedMaster != null) {
        
      var appliedMaster = app.activeDocument.pages[i].appliedMaster.name;
        
      }


        
      // if it's not an advertising page, get the page number and add it to an array containing page numbers to export
        
      if (appliedMaster !== "H-ADVERTISING" && appliedMaster!= "[None]" && appliedMaster!= null) {
        alert
      (appliedMaster);
        pages
      .push(app.activeDocument.pages[i].name);
        printpages
      = pages.join(",");
        
      // set the pageString of pages to export as jpegs
        app
      .jpegExportPreferences.pageString = printpages;
        
      // export all the pages using the export page range page string
        c
      .exportFile(ExportFormat.JPG, File(Folder.desktop + "/EDIT_Jpgs/" + myDocumentName));

        
      }

        
      }

        
      };

        • 1. Re: Trying to filter a jpeg export by Master Page
          Laubender Adobe Community Professional & MVP

          Hi,

          work with logical OR and not with logical AND.
          You also could do it in a positive way.

           

          Some code to illustrate this:

           

          // Have one document open:
          var doc = app.documents[0];
          
          var nameToAvoid = "H-ADVERTISING";
          
          // Building an array of pages:
          var pagesArray = doc.pages.everyItem().getElements();
          var pagesLength = pagesArray.length;
          
          // Loop through the pages array:
          for(var n=0;n<pagesLength;n++)
          {
              // Use logical OR here and do it in a positive way:
              if(pagesArray[n].appliedMaster == null || pagesArray[n].appliedMaster.name == nameToAvoid)
              {continue}; // Go on looping immediately
          
              // In case the master is not null
              // or the page's applied master has not the name we want to avoid
              // invoke a function that is declared below.
             
              // The one argument that goes into that function is a page object.
             
              doSomething(pagesArray[n]);
          };
          
          function doSomething(/*object Page*/page)
          {
              $.writeln(page.name+"\t"+page.appliedMaster+"\t"+page.appliedMaster.name);
             
              /*
                  Do your export to JPEG here, if you like.
              */
          };
          
          /*
              Result from my German InDesign (masterpage = Musterseite)
              written to the JavaScript Console of the ESTK:
             
              1    [object MasterSpread]    A-Musterseite
              3    [object MasterSpread]    A-Musterseite
              4    [object MasterSpread]    A-Musterseite
              7    [object MasterSpread]    A-Musterseite
              8    [object MasterSpread]    A-Musterseite
              10    [object MasterSpread]    A-Musterseite
              11    [object MasterSpread]    A-Musterseite
              12    [object MasterSpread]    A-Musterseite
          */
          

           

          Regards,
          Uwe

          1 person found this helpful
          • 2. Re: Trying to filter a jpeg export by Master Page
            absolutely23 Level 1

            I marked the answer as the correct answer because it's a much more elegant solution (using the OR instead of AND operator ), but one strange thing that happened is that when I updated to InDesign 2017, the code was working again.

            • 3. Re: Trying to filter a jpeg export by Master Page
              Laubender Adobe Community Professional & MVP

              Hm, what was your exact InDesign version before you updated to CC 2017?

               

              Regards,
              Uwe

              • 4. Re: Trying to filter a jpeg export by Master Page
                absolutely23 Level 1

                It was CC 2015, and it was working for a bit, but there are periodic updates that happened before the major CC 2017 update that may have tripped the code up.