0

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.

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

    }

      }

    };
JPP
  • 15
  • 5
  • So, just at a glance, it is _possible_ you are running into issues around the single equal not comparison (`!=`) instead of the double equal not comparison (`!==`). The former does automatic type-conversion under the hood, which can yield some unexpected results. This is just something to try, though-- I didn't do a thorough scouring of your code. – Alexander Nied Oct 25 '16 at 00:53
  • 5
    there's no such thing as an "if loop" - just sayin – Jaromanda X Oct 25 '16 at 00:55
  • Also try writing out the appliedMaster variable to the log or alert and compare it... make sure it's all uppercase and no extra spaces etc? (to match your expectation of what it should be) – Michael Hudson Oct 25 '16 at 00:56
  • I edited the code and tried both the suggestions above. What's happening is that it's alerting the correct value of appliedMaster, i.e. if I were to change it to == it would alert "H-ADVERTISING" but as it is now it alerts every other value of appliedMaster except "H-ADVERTISING" but the code below seems to be executed no matter what. – JPP Oct 25 '16 at 01:33
  • Your local `var appliedMaster` inside the `for` loop could be hiding its value from the global variable with the same name. Oh wait – but you don't even have a global with that name. Then put `var appliedMaster = null;` somewhere near the start. – Jongware Oct 25 '16 at 09:07

2 Answers2

0

Note: Property 'pageString" is valid when JPEG export range is not all. So - just to be sure - you may need to set app.jpegExportPreferences.jpegExportRange to 'ExportRangeOrAllPages.EXPORT_RANGE'

Note2: Consider that a book could be opened without opened docs OR an activeDocument could be from outside of app.books[0] ==> in this case your loop for (i = 0; i < app.activeDocument.pages.length; i++) possibly lead to wrong values, cause target doc is opened... inside this loop.

Jarek

Cashmirek
  • 269
  • 1
  • 9
0

Okay, the script is working now, after I updated to InDesign 2017. So, not really an answer, other than there must have been a bug in InDesign. The only thing I did tweak in the code was to add this

        if (app.activeDocument.pages[i].appliedMaster !== null && app.activeDocument.pages[i].appliedMaster!== "H-ADVERTISING" && app.activeDocument.pages[i].appliedMaster!==null) {
           appliedMaster = app.activeDocument.pages[i].appliedMaster.name;
        }

earlier in the code which checked the actual value of the master page, instead of checking a variable that was assigned the value of the master page. This seemed to be the trick to checking to filter for any pages that had no master page applied (null or "[None]")

JPP
  • 15
  • 5
  • I would also avoid relying on underlaying js conversions processes such as comparing a MasterSpread instance to a String object. This is what happens when you do …pages[i].appliedMaster!== "H-ADVERTISING"… So you may either want to do pages[i].appliedMaster.name!== "H-ADVERTISING" or pages[i].appliedMaster!== doc.masterSpreads.item("H-ADVERTISING"). But the former is probably safer – Loic Nov 15 '16 at 09:08