1 Reply Latest reply on Jan 28, 2009 11:03 AM by (mps)

    Loop through all open documents not performing some actions

    Level 1
      I have a loop that imports XML, then Exports each page individually, then has a SaveAs dialog for the indd, then closes the active document.

      The import XML works, the SaveAs works, and the close works but the loop skips over the PDF part. Is it because I need to define myCounter in the for loop within the ExportPDF function? I just included the part from Choose XML to the end of ExportPDF.

      //choose XML
      var FilterXML = "XML File: *.xml";
      var myXML = File.openDialog("Choose XML File", FilterXML);

      //loop for each action below
      for(myDocumentCounter = app.documents.length; myDocumentCounter > 0; myDocumentCounter--){

      //import XML
      app.activeDocument.importXML(myXML);

      //PDF by page to settings; preset must be "dailies_x1a"
      function myExportPages(myFolder){
      var myPageName, myFilePath, myFile;
      var myDocument = app.activeDocument;
      var myDocumentName = myDocument.name;
      var myBaseName = myBaseNameField.editContents;
      for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
      myPageName = myDocument.pages.item(myCounter).name;
      app.pdfExportPreferences.pageRange = myPageName;
      //The name of the exported files will be the base name + the page name + ".pdf".
      //If the page name contains a colon (as it will if the document contains sections), then remove the colon.
      var myRegExp = new RegExp(":","gi");
      myPageName = myPageName.replace(myRegExp, "_");
      myFilePath = myFolder + "/" + myBaseName + "_" + myPageName + ".pdf";
      myFile = new File(myFilePath);
      myDocument.exportFile(ExportFormat.pdfType, myFile, false, "dailies_x1a");
      }
      }
        • 1. Re: Loop through all open documents not performing some actions
          Level 1
          Nevermind, I found my error. Code should read...

          for(myDocumentCounter = app.documents.length; myDocumentCounter > 0; myDocumentCounter--){

          if(app.documents.length != 0){
          var myFolder = Folder.selectDialog ("Choose a Folder to Save the PDFs");
          if(myFolder != null){
          myExportPages(myFolder);
          }
          }
          else{
          alert("Please open a document and try again.");
          }
          function myExportPages(myFolder){
          var myPageName, myFilePath, myFile;
          var myDocument = app.activeDocument;
          var myDocumentName = myDocument.name;
          var myDialog = app.dialogs.add();
          with(myDialog.dialogColumns.add().dialogRows.add()){
          staticTexts.add({staticLabel:"Please Modify the Base Name if Necissary:"});
          var myBaseNameField = textEditboxes.add({editContents:myDocumentName,
          minWidth:160});
          }
          var myResult = myDialog.show({name:"ExportPages"});
          if(myResult == true){
          var myBaseName = myBaseNameField.editContents;
          //Remove the dialog box from memory.
          myDialog.destroy();
          for(var myCounter = 0; myCounter < myDocument.pages.length;
          myCounter++){
          myPageName = myDocument.pages.item(myCounter).name;
          app.pdfExportPreferences.pageRange = myPageName;
          //The name of the exported files will be the base name + the
          //page name + ".pdf".
          //If the page name contains a colon (as it will if the
          //document contains sections),
          //then remove the colon.
          var myRegExp = new RegExp(":","gi");
          myPageName = myPageName.replace(myRegExp, "_");
          myFilePath = myFolder + "/" + myBaseName + "_" + myPageName + ".pdf";
          myFile = new File(myFilePath);
          myDocument.exportFile(ExportFormat.pdfType, myFile, false, "dailies_x1a");
          }
          }
          else{
          myDialog.destroy();
          }
          }