9 Replies Latest reply on Aug 13, 2008 2:57 PM by John.Kordas

    Print postscript script giving error on .print(false) JS CS3

    John.Kordas Level 1
      I found this example here:

      Olav Kvern, "Need script for printing Postscript files (ID CS2)" #4, 19 Nov 2007 4:45 pm

      But if the ID doc has a blank page an error on the myDocument.print(flase); will occur.

      The message I'm getting is:

      The configuration was invalid or the user cancelled. Please try the same operation in the user interface to see more detailed information.

      Once you get rid of the blank or add something to the blank the error is gone. Does anyone know how to stop the error if a user adds a blank page?

      Cheers.
        • 1. Re: Print postscript script giving error on .print(false) JS CS3
          Level 1
          Dear John

          Ive a suggestion to solve this problem may be this will help you

          First get the count of pageitems of the page which you are going to print, if the item count is 0 then that page is a blank page. After that you decide about skip or print, if you want print the blank page just draw a text frame and insert a white space inside the frame and give print out
          • 2. Re: Print postscript script giving error on .print(false) JS CS3
            Level 1
            Hi John, priyan,

            I think that turning on the printBlankPages property in printPreferences will avoid this problem. You should also be able to trap the error using try...catch if you don't want to print the blank pages.

            Thanks,

            Ole
            • 3. Re: Print postscript script giving error on .print(false) JS CS3
              John.Kordas Level 1
              Thanks priyan, Olav,

              I will try your suggestion priyan. If I can check the page itme and get back a null result I should be able to get the for loop to skip to the next page correct?

              Olav hope you don't mind but I made a slight change to give the user an option to print a specific page if required. Just a warning the script does not check that the number you enter is part of the doc. So if you doc range is from pages 19 to 24 and you try and print page 2 it will error.

              For anyone else out there that finds this useful you can also change the browse option to a drop menu and set the paths for output. This will speed up the process if you have set paths.

              Anyway here is the alteration.

              //PrintEachPageAsPS.jsx
              //An InDesign CS3 JavaScript
              //
              //Prints each page of an InDesign document as a PostScript file to
              //a selected folder.
              main();
              function main(){
              if(app.documents.length != 0){
              //Display a "choose folder" dialog box.
              var myFolder = Folder.selectDialog ("Choose a Folder");
              if(myFolder != null){
              myExportPages(myFolder);
              }
              }
              else{
              alert("Please open a document and try again.");
              }

              }
              function myExportPages(myFolder){
              var myPageName, myFilePath, myFile;
              var myDocument = app.documents.item(0);
              var myDocumentName = myDocument.name;
              var myDialog = app.dialogs.add();
              with(myDialog.dialogColumns.add()){
              staticTexts.add({staticLabel:"Base name:"});
              staticTexts.add({staticLabel:"Print only this page:"});
              with(myDialog.dialogColumns.add()){
              var myBaseNameField = textEditboxes.add({editContents:myDocumentName.substring(0,4), minWidth:160});
              var myPageNumberField = integerEditboxes.add({editContents:"", minWidth:80});
              }
              }

              var myResult = myDialog.show({name:"PrintEachPageAsPS"});
              if(myResult == true){
              var myBaseName = myBaseNameField.editContents;
              var myPageNum = myPageNumberField.editValue;
              //Remove the dialog box from memory.
              myDialog.destroy();
              //Specify PostScript file and device-independent PostScript.
              myDocument.printPreferences.printer = Printer.POSTSCRIPT_FILE;
              myDocument.printPreferences.ppd = PPDValues.DEVICE_INDEPENDENT;
              if (myPageNum > 0){
              myPageName = myPageNum+ "";
              myDocument.printPreferences.pageRange = myPageName;
              var myRegExp = new RegExp(":","gi");
              myPageName = myPageName.replace(myRegExp, "_");
              myFilePath = myFolder + "/" + myPageName + "_" + myBaseName + ".ps";
              myFile = new File(myFilePath);
              document.printPreferences.printFile = myFile;
              myDocument.print(false);
              }else{
              for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
              myPageName = myDocument.pages.item(myCounter).name;
              myDocument.printPreferences.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 + "/" + myPageName + "_" + myBaseName + ".ps";
              myFile = new File(myFilePath);
              document.printPreferences.printFile = myFile;
              myDocument.print(false);
              }

              }
              alert("Done!");
              }
              else{
              myDialog.destroy();
              }
              }
              • 4. Re: Print postscript script giving error on .print(false) JS CS3
                John.Kordas Level 1
                Just trying to check that the page entered is in the page range and so far I can get a confirmation by:

                var testnum = 25;

                for (var myCounter = 0; myCounter < app.activeDocument.pages.length; myCounter++){
                var pagename = app.activeDocument.pages.item(myCounter).name
                if (testnum == parseInt(pagename)){
                alert("yes");
                }
                }

                if I add

                else{
                alert("no");
                }

                If the page range is 5 pages then you get 4 no alerts and 1 yes.

                Would it be possible to make the page numbers into an array and then check is the testnum is within that array?

                Any suggestion as to what would be the best way to do this check?
                • 5. Re: Print postscript script giving error on .print(false) JS CS3
                  John.Kordas Level 1
                  I've worked out that I can check to see if the number falls with the page range by:

                  var testnum = 22;

                  var numofpages = app.activeDocument.pages.length - 1;
                  var firstpage = app.activeDocument.pages[0].name;
                  var lastpage = app.activeDocument.pages[numofpages].name;

                  if (testnum >= firstpage && testnum <= lastpage){
                  alert("that page is within our range");
                  }else{
                  alert("that page does not exist");
                  }

                  The problem is if my document is made up of 5 pages for example pages 19, 20 24, 25, 26 if the user types in page 21 it is within the range by is not in the doc.
                  • 6. Re: Print postscript script giving error on .print(false) JS CS3
                    Level 1
                    Hi John,

                    The thing about page.name (which is what the page range takes) is that it's a string, not a number. It can even contain characters that come from the section numbering. This means that the only save way to test is to get an array of page number strings using something like this:

                    var myPageNumbers = app.documents.item(0).pages.everyItem().name;

                    And then have a function iterate through the array, comparing each array item to the string the user entered. As soon as you find it, stop iterating and return true. If you reach the end of the array without finding it, return false. Luckily, this is fairly quick.

                    Thanks,

                    Ole
                    • 7. Re: Print postscript script giving error on .print(false) JS CS3
                      John.Kordas Level 1
                      Thanks Ole,

                      I just got this to work. Need to run a few more tests but so far it's good.

                      Change this line:

                      myDocument.printPreferences.pageRange = myPageName;

                      to:

                      try{
                      myDocument.printPreferences.pageRange = myPageName;
                      }
                      catch(e){
                      alert("Page "+myPageNum+ " does not exist. Try again.");
                      }

                      I see what you mean about catching the error.

                      NOTE: I just found that the script continues even though I get the error. Is there a way I can stop it?

                      Cheers, John.
                      • 8. Re: Print postscript script giving error on .print(false) JS CS3
                        John.Kordas Level 1
                        To stop the script after the alert I just used return:

                        try{
                        myDocument.printPreferences.pageRange = myPageName;
                        }
                        catch(e){
                        alert("Page "+myPageNum+ " does not exist. Try again.");
                        return;
                        }

                        Hope this helps.
                        • 9. Re: Print postscript script giving error on .print(false) JS CS3
                          John.Kordas Level 1
                          To stop the blank page error you can also use:

                          try{
                          myDocument.print(false);
                          }
                          catch(e){
                          alert("Page "+myPageName+" may be blank plesae check.");
                          }

                          By not putting a return after the alert the script will continue on with the other pages.