4 Replies Latest reply on May 16, 2014 2:22 AM by markc0

    InDesign javaScript

    markc0 Level 1



      I have chnaged the Javascript called PlaceMultiplepagePDF that comes free with InDesign so that i dont get any Prompts (script below) it just places PDFs onto the layout.

      I have also changed it so that the document setup is different , however the problem lie because the PDF gets attached to the indesign page in the top left corner of the Page (see attachment).

      Is there anyway that javascript can set where the PDF lands on the Page?

      I.e i would like the pDF to be placed where the margins start and stop



          //An InDesign CS3 JavaScript 


          //Places all of the pages of a multi-page PDF. 


          //For more on InDesign scripting, go to http://www.adobe.com/products/indesign/xml_scripting.html 

          //or visit the InDesign Scripting User to User forum at http://www.adobeforums.com 


          //Display a standard Open File dialog box. 

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

          //var myPDFFile = File("~/Desktop/guides/GMG Contact.pdf")

          var myPDFFile = File("~/Desktop/guides/Tatler_20140403_MAY14_reduced.pdf")

          //var myPDFFile = File("~/Desktop/guides/acrobat-10-quick-reference-Production.pdf")

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

               var myDocument, myPage; 

              if(app.documents.length != 0){ 

                  myDocument, myNewDocument = myChooseDocument(); 



                    myDocument = app.documents.add(); 

                    myNewDocument = false; 


              if(myNewDocument == false){ 

                   myPage = myChoosePage(myDocument); 



                  myPage = myDocument.pages.item(0); 


             myPlacePDF(myDocument, myPage, myPDFFile); 





          function myChoosePage(myDocument){ 

              var myPageNames = new Array; 

              //myPageNames.push("New Page");//adds a new page to the display dialog below

              //Get the names of the pages in the document 

              for(var myCounter = 0; myCounter < myDocument.pages.length;myCounter++){ 



              var myChoosePageDialog = app.dialogs.add({name:"Choose a Page", canCancel:false}); 




                          //staticTexts.add({staticLabel:"Place PDF on:"}); 



              //var myChoosePageDropdown = dropdowns.add({stringList:myPageNames, selectedIndex:0}); 

                  var myChoosePageDropdown = dropdowns.add({stringList:myPageNames, selectedIndex:0}); 





              var myPage = myDocument.pages.item(myChoosePageDropdown.selectedIndex);

      myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;

      myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

      myDocument.documentPreferences.intent = DocumentIntentOptions.DPS_INTENT;

      //myDocument.documentPreferences.intent = DocumentIntentOptions.WEB_INTENT;

      myDocument.documentPreferences.pageHeight = 1024;

      myDocument.documentPreferences.pageWidth = 768;

      myDocument.documentPreferences.facingPages = false;

      var myMasterSpread = myDocument.masterSpreads.item(0);

      var myBreak = false;

      var myMarginPreferences = myMasterSpread.pages.item(0).marginPreferences;

      myMarginPreferences.top = 14.5;

      myMarginPreferences.left = 0;

      myMarginPreferences.bottom = 14.5;

      myMarginPreferences.right = 0;

      //myMarginPreferences.gutter = 0;



      return myPage;


          //function myPlacePDF(myDocument, myPage, myPDFFile){ 

      function myPlacePDF(myDocument, myPage, myPDFFile){ 

               var myPDFPage = app.activeDocument;

               var graphics = myPDFPage.allGraphics;

               app.pdfPlacePreferences.pdfCrop = PDFCrop.crop; 

               for (var i = 0; i < graphics.length; i++) {

           image = graphics[i];

           image.absoluteHorizontalScale = 117.797; // Horizontal Scale

           image.absoluteVerticalScale = 100; // Vertical Scale




               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; 



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


                              myBreak = true; 



                    myCounter = myCounter + 1; 


      var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

         //var saveFile= File("~/Desktop/guides/" + Name + ".indd")

      var saveFile= File("~/Desktop/guides/Test" + Name + ".indd")  






      Screen Shot 2014-05-14 at 13.00.48.png

        • 1. Re: InDesign javaScript
          Laubender Adobe Community Professional & MVP

          @markc0 – yes. That can be done. You have to look for that line of code where the actual placing of the individual PDF page is done:


          Line 90 in the original CS3 script:


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


          The place function is fed with two arguments separated by a comma.

          1. The File to place in the file system of your operating system

          2. An array with two numbers


          The array with two numbers should lead you to the conclusion: ah, that could be the x/y position of the placed PDF page.

          Just try to change the numbers here. Change it to what? The upper border of the margins and the left border of the margins.

          Where do we find these? E.g. in the marginPreferences of the page.


          So we could write instead of the line above:


          var upperLeftXY = [myPage.marginPreferences.left,myPage.marginPreferences.top];
          myPDFPage = myPage.place(File(myPDFFile), upperLeftXY)[0];


          With CS3 this will work. Just tested.

          The script will place the PDF pages to that position instead of [0,0] in the corner of the page.


          Now resizing the placed PDF pages is a different thing.
          How would you like that beeing done?


          Proportionally to the width of the margins? Moved to the center of the page? ??



          • 2. Re: InDesign javaScript
            markc0 Level 1

            Hi many Thanks

            Yes i would like the resizeing done to all margins on the page

            i.e from left to right margins and top to bottom.


            Many Thanks

            • 3. Re: InDesign javaScript
              Laubender Adobe Community Professional & MVP

              @markc0 – be careful what you are wishing for ;-)


              If the placed PDF page will be the size of the rectangle all margins are forming, it's more than likely, that the aspect ratio in not the one from the original page…



              • 4. Re: InDesign javaScript
                markc0 Level 1

                HI how can i change the aspect ratio then please?

                I have also noticed with my script that the Margins are not setup correctly on the first page but are on all other pages after the first page, any chnace you could see where im going wrong?