8 Replies Latest reply on Sep 1, 2016 9:24 AM by Awitmer

    Modify an Indesign script for me?

    Awitmer Level 1

      Hello all!

       

      I'm am fairly new to Indesign scripting and I have a script here that I would like to have modified.

      This script places a multi-page pdf in an Indesign file and adds pages as necessary. Right now this script pops up a dialog box that asks which document I want the pdf to be placed in and I would like to have that dialog box eleminated and have the script automatically place the pdf in the document that I have active. Also this script asks which page to start placing the pdf on and I would like if it simply would just automatically start on the first page of the active Indesign document without even asking me. Would someone be willing to make those modifications to the script below and post the code on this thread? Let me know if I did not make something clear.

       

      var myDocument = app.activeDocument;
      //Get the current page.
      main();
      function main(){
        //Display a standard Open File dialog box.
        var myPDFFile = File.openDialog("Choose a PDF File");
        if((myPDFFile != "")&&(myPDFFile != null)){
        var myDocument, myPage;
        if(app.documents.length != 0){
        var myTemp = myChooseDocument();
        myDocument = myTemp[0];
        myNewDocument = myTemp[1];
        }
        else{
        myDocument = app.documents.add();
        myNewDocument = false;
        }
        if(myNewDocument == false){
        myPage = myChoosePage(myDocument);
        }
        else{
        myPage = myDocument.pages.item(0);
        }
        myPlacePDF(myDocument, myPage, myPDFFile);
        }
      }
      function myChooseDocument(){
          var myDocumentNames = new Array;
          myDocumentNames.push("New Document");
          //Get the names of the documents
          for(var myDocumentCounter = 0;myDocumentCounter < app.documents.length; myDocumentCounter++){
              myDocumentNames.push(app.documents.item(myDocumentCounter).name);
          }
          var myChooseDocumentDialog = app.dialogs.add({name:"Choose a Document", canCancel:false});
          with(myChooseDocumentDialog.dialogColumns.add()){
              with(dialogRows.add()){
                  with(dialogColumns.add()){
                      staticTexts.add({staticLabel:"Place PDF in:"});
                  }
                  with(dialogColumns.add()){
                      var myChooseDocumentDropdown = dropdowns.add({stringList:myDocumentNames, selectedIndex:0});
                  }
              }
          }
        var myResult = myChooseDocumentDialog.show();
        if(myResult == true){
        if(myChooseDocumentDropdown.selectedIndex == 0){
        myDocument = app.documents.add();
        myNewDocument = true;
        }
        else{
        myDocument = app.documents.item(myChooseDocumentDropdown.selectedIndex-1);
        myNewDocument = false;
        }
        myChooseDocumentDialog.destroy();
        }
        else{
        myDocument = "";
        myNewDocument = "";
        myChooseDocumentDialog.destroy();
        }
          return [myDocument, myNewDocument];
      }
      function myChoosePage(myDocument){
          var myPageNames = new Array;
          //Get the names of the pages in the document
          for(var myCounter = 0; myCounter < myDocument.pages.length;myCounter++){
              myPageNames.push(myDocument.pages.item(myCounter).name);
          }
          var myChoosePageDialog = app.dialogs.add({name:"Choose a Page", canCancel:false});
          with(myChoosePageDialog.dialogColumns.add()){
              with(dialogRows.add()){
                  with(dialogColumns.add()){
                      staticTexts.add({staticLabel:"Place PDF on:"});
                  }
                  with(dialogColumns.add()){
                      var myChoosePageDropdown = dropdowns.add({stringList:myPageNames, selectedIndex:0});
                  }
              }
          }
          myChoosePageDialog.show();
          var myPage = myDocument.pages.item(myChoosePageDropdown.selectedIndex);
          myChoosePageDialog.destroy();
          return myPage;
      }
      function myPlacePDF(myDocument, myPage, myPDFFile){
        var myPDFPage;
        app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
        var myCounter = 1;
        var myBreak = false;
        while(myBreak == false){
        if(myCounter > 1){
        myPage = myDocument.pages.add(LocationOptions.after, myPage);
        }
        app.pdfPlacePreferences.pageNumber = myCounter;
      
           myPDFPage = myPage.place(File(myPDFFile), [0,0])[0];
        if(myCounter == 1){
        var myFirstPage = myPDFPage.pdfAttributes.pageNumber;
        }
        else{
        if(myPDFPage.pdfAttributes.pageNumber == myFirstPage){
        myPage.remove();
        myBreak = true;
        }
        }
        myCounter = myCounter + 1;
        }
      }
      var myPDFFrame =  myPlacePDF.parent;
      

       

      Thanks a lot in advance for your help! I greatly appreciate it!

        • 1. Re: Modify an Indesign script for me?
          tpk1982 Level 4

          I think You need to have a hot folder and place the PDF file in the folder.. so that every time script will take PDF from that particular folder.. that is possible for me

           

          May be some other experts in forum will give you more suggestion..

          • 2. Re: Modify an Indesign script for me?
            Awitmer Level 1

            Thanks so much for the reply!

             

            I had not thought of that doing something like that with a hot folder. I would interested in finding out what the code for something like that would be.

             

            I still would like though if somebody could remove the dialog boxes from the script above if possible and post the code on this thread.

             

            Thanks again!

            • 3. Re: Modify an Indesign script for me?
              Skemicle Level 3

              Here is a rewrite for you. I've also added a bit of code to remove empty pages from the document.

               

               

              /*

              PlaceMulitpagePDF Script condensed by Skemicle

              */

              if (parseFloat(app.version) < 6)

              main();

              else

              app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Place Multipage PDF");

              function main() {

                  var myPDFFile = File.openDialog("Choose a PDF File");

                  if((myPDFFile != "")&&(myPDFFile != null)){

                      var myDocument = app.activeDocument,

                      myPage = myDocument.pages.item(0);

                      var myPDFPage;

                      app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;

                      var myCounter = 1;

                      var myBreak = false;

                      while(myBreak == false){

                          if(myCounter > 1){

                              myPage = myDocument.pages.add(LocationOptions.after, myPage);

                          }

                          app.pdfPlacePreferences.pageNumber = myCounter;

                          myPDFPage = myPage.place(File(myPDFFile), [0,0])[0];

                          if(myCounter == 1){

                              var myFirstPage = myPDFPage.pdfAttributes.pageNumber;

                          }else{

                              if(myPDFPage.pdfAttributes.pageNumber == myFirstPage){

                                  myPage.remove();

                                  myBreak = true;

                              }

                          }myCounter = myCounter + 1;

                      }

                  }var pages = app.activeDocument.pages;

                  for(c=0;c<pages.length;c++){

                      if(pages[c].pageItems.length == 0){

                          pages[c].remove();

                      }

                  }

              }

              • 4. Re: Modify an Indesign script for me?
                Awitmer Level 1

                Thank you very much! This is exactly what I wanted!

                 

                Thanks so much for the reply!

                 

                Where did you get your knowledge of scripting? Do you have books or courses that you could recommend?

                • 5. Re: Modify an Indesign script for me?
                  Skemicle Level 3

                  To be honest I just started really writing javascripts about two months ago from a thread at https://indesignsecrets.com/topic/cut-photo-in-half-along-curved-path.. From there i would open the prepackaged indesign scripts and find out why the scripts do what they do. And if i needed something scripted that i wasn't sure how to do i would find a script that would something like what i wanted and find where in the script it did what i wanted and pull that for my own scripts.

                   

                  With this particular script i noticed that each dialog box had it's own function so i found the call to each function and replaced it and went through to see what all i could remove from the scripts while still letting it do it's job.

                  • 6. Re: Modify an Indesign script for me?
                    Awitmer Level 1

                    Very well! I was just kinda curious as I have just started into scripting myself.

                     

                    Thanks again for your help and replies! I greatly appreciate it!

                     

                    Have a great day!

                    • 7. Re: Modify an Indesign script for me?
                      Skemicle Level 3

                      For future reference, instead of just posting a code you want modified, post some code that you have tried to modify yourself as well. If you really want to learn; the best way is to just do it. Try to modify some codes for yourself and post your attempts. Then someone else can see what you have tried and be more likely to help you and to let you know what mistakes you have made and provide a solution so you can better understand how to write the code.

                      • 8. Re: Modify an Indesign script for me?
                        Awitmer Level 1

                        Thanks for the tips!