15 Replies Latest reply on Feb 10, 2013 2:09 AM by Colin Flashman

    Need help in automating batch placing of images on spreads

    Ruq Level 1

      My workflow is :

      • I have jpeg files (mostly 100+).
      • They are the size of 2 pages (e.g. 1 spread per jpeg file)
      • I need to place them on either even or odd page (aligned Top-Left).

      Currently I’m doing this work manually.
      Need help to automate this.

      Thanks and regards.

        • 1. Re: Need help in automating batch placing of images on spreads
          Jump_Over Level 5

          Hi,

           

          1st aid:

          you could use ImageCatalog.jsx from application script samples.

          or

          you could modify it

          or

          you could create your own one...

           

          so are you looking for help or someone who just can do it?

           

          rgds

          • 2. Re: Need help in automating batch placing of images on spreads
            Ruq Level 1

            Thanks for the help.

             

            I tried Image Catalog script. The script places each file on its own page, whereas i want to place one image per spread.

             

            I have no experience in InDesign Scripting.

            Will be very grateful if someone from the scripting gurus can help me out by making a simple and useful script.

             

            Kindly help.

             

            Thanks and regards.

            • 3. Re: Need help in automating batch placing of images on spreads
              Ruq Level 1

              I tried my hands on JavaScript in InDesign for the first time, and thankfully I was able to make a script which did what I was doing manually.

               

              Will post the script here after proper testing and debugging.

               

              Thanks and regards.

              • 4. Re: Need help in automating batch placing of images on spreads
                Abigailrstahlman

                Hi,

                 

                I have a very similar workflow. I have tried to edit the ImageCatalog.jsx myself, however, being a total JavaScript novis, I have had no luck.

                 

                Because I want to work within my active document, I am trying to alter line 236 -

                 

                var myDocument:Document = app.documents.add();

                to

                var myDocument:Document = app.activeDocument;

                 

                Of course, when I change this line, the rest of the script malfunctions susseccively. Currently I am running into confusion at line 241 -

                 

                var myNumberOfPages = Math.round(myNumberOfFrames / myFramesPerPage);

                    if ((myNumberOfPages * myFramesPerPage) < myNumberOfFrames){ 

                        myNumberOfPages++;

                    }

                 

                If you edited an existing script, I would love some further explaination.

                If you created a whole new script, I would be grateful for any resources you used as reference.

                If you could post your script I would very much appreciate that too.

                 

                Thanks!

                • 5. Re: Need help in automating batch placing of images on spreads
                  Ruq Level 1

                  As I mentioned earlier I have very little experience with JavaScript.

                   

                  I also copied the ImageCatalog script to a new file and read it atleast five times, and tried to wrap my head around the flow of execution.

                  The comments were really helpful in the process.

                   

                  I proritized what i want to achive with my script and started deleting unwanted code. I also tested it frequently.

                   

                  Here is my script as it stands right now.

                   

                  function Init(){

                              

                          var myFilteredFiles;

                          //Make certain that user interaction (display of dialogs, etc.) is turned on.

                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                          myExtensions = [".jpg", ".jpeg"]

                          //Display the folder browser.

                          var myFolder = Folder.selectDialog("Select the folder containing the images", "");

                          //Get the path to the folder containing the files you want to place.

                          if(myFolder != null){

                                      if(File.fs == "Macintosh"){

                                          myFilteredFiles = myMacOSFileFilter(myFolder);

                                      }

                                      else{

                                          myFilteredFiles = myWinOSFileFilter(myFolder);

                                      }

                                      if(myFilteredFiles.length != 0){

                                              myPlaceImages(myFilteredFiles);

                                              alert("Done!");

                                      }

                                }

                          }

                   

                   

                  // call the initial function

                  Init();

                   

                   

                  //Windows version of the file filter.

                  function myWinOSFileFilter(myFolder){

                            var myFiles = new Array;

                            var myFilteredFiles = new Array;

                            for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++){

                                      myExtension = myExtensions[myExtensionCounter];

                          myFiles = myFolder.getFiles("*"+ myExtension);

                                      if(myFiles.length != 0){

                                                for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++){

                                                          myFilteredFiles.push(myFiles[myFileCounter]);

                                                }

                                      }

                            }

                            return myFilteredFiles;

                  }

                  function myMacOSFileFilter(myFolder){

                            var myFilteredFiles = myFolder.getFiles(myFileFilter);

                            return myFilteredFiles;

                  }

                  //Mac OS version of file filter

                  //Have to provide a separate version because not all Mac OS users use file extensions

                  //and/or file extensions are sometimes hidden by the Finder.

                  function myFileFilter(myFile){

                            var myFileType = myFile.type;

                            switch (myFileType){

                                      case "JPEG":

                          return true;

                                                break;

                                      default:

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

                                                var myExtension = myExtensions[myCounter];

                                                if(myFile.name.indexOf(myExtension)>-1){

                                                          return true;

                                                          break;

                                                }

                                      }

                            }

                            return false;

                  }

                   

                   

                  function myPlaceImages(myFiles){

                          var myDocument = app.documents.item(0);

                          var myDocumentPreferences = myDocument.documentPreferences;        

                         

                          var myNumberOfFiles = myFiles.length;

                         

                          alert(myNumberOfFiles);

                         

                          var myNumberOfPages = (myNumberOfFiles * 2) + 1;

                          myDocumentPreferences.pagesPerDocument = myNumberOfPages;       

                         

                         

                          var myPage = myDocument.pages.item(0);

                          var myPages = myDocument.pages;

                           

                         

                          for (myCounter = myDocument.pages.length-1; myCounter >= 0; myCounter--){ 

                              if(myCounter%2 == 1){

                                   myPage = myPages.item(myCounter);

                                  var myLiveWidth = myDocumentPreferences.pageWidth

                                  var myLiveHeight = myDocumentPreferences.pageHeight

                      

                                  myY1 = 0

                                  myY2 = myLiveHeight

                   

                   

                                  myX1 = 0

                                  myX2 = myLiveWidth * 2

                                  myRectangle = myPage.rectangles.add(myDocument.layers.item(-1), undefined, undefined, {geometricBounds:[myY1, myX1, myY2, myX2], strokeWeight:0, strokeColor:myDocument.swatches.item("None")}); 

                          }

                      }

                          for (myCounter = 0; myCounter < myNumberOfFiles; myCounter++){ 

                             

                              myFile = myFiles[myCounter]; 

                              //alert(myFile);           

                              myRectangle = myDocument.rectangles.item(myCounter);

                              myRectangle.place(File(myFile));

                          }

                   

                   

                                      for (var myCounter = myDocument.rectangles.length-1; myCounter >= 0;myCounter--){ 

                                                if (myDocument.rectangles.item(myCounter).contentType == ContentType.unassigned){

                                                          myDocument.rectangles.item(myCounter).remove();

                                                } 

                                                else{

                                                          //As soon as you encounter a rectangle with content, exit the loop.

                                                          break;

                                                }

                                      }

                   

                  }

                  • 6. Re: Need help in automating batch placing of images on spreads
                    [Jongware] Most Valuable Participant

                    Abigail,

                     

                    This is the reason the script doesn't work for you:

                     

                    myNumberOfPages++;

                     

                    Reason is, the standard script creates a *new* document; and when creating one, you can specify the number of pages, just like you can in the UI. So, since the script knows how many images there are in total (the result of reading a folder full of them) and how many images there are on a single page, it's able to create *just* enough empty pages to fill them all.

                     

                    If you are sure that you already have enough pages in your existing document, you can delete this and the preceding line. However, if you don't have enough pages, the script will fill them all and then ... stop with an error.

                     

                    It's hard for someone else to come up with a custom script that works *exactly* like you want it to, and within the constraints you have (i.e., page count, document size, fonts and styles used ... et cetera). That's why I would urge you to go ahead and try to adjust the existing script.

                     

                    Tailoring a script to your personal needs is way more easier than to write one from scratch. Besides, when your confidence grows you'll find how to add all sorts of extra functions that an external script writer would never have thought of.

                    • 7. Re: Need help in automating batch placing of images on spreads
                      Jump_Over Level 5

                      Hi,

                       

                      Going further this road:

                       

                      A main "working" function here is myPlaceImages(myFiles)

                      It could look like this (built from zero point):

                       

                      function myPlaceImages(myFiles){
                          var myCounter = myFiles.length;
                          app.activeDocument.documentPreferences.properties = 
                              {pagesPerDocument: myCounter*2,
                                  facingPages: true,
                                  allowPageShuffle: true};
                          app.activeDocument.pages[0].appliedSection.properties =
                              {continueNumbering:false, pageNumberStart: 2};
                      //above is cause 1st spread can be 2-pages
                          var mySpreads = app.activeDocument.spreads;
                          var Pbounds = mySpreads[0].pages[1].bounds;
                      // each rectangle has same size: from [0,0] point to bottom right corner of spread
                          var Rbounds = [ 0, 0, Pbounds[2], Pbounds[3] ];
                          for (var k = 0; k < myCounter; k++) {
                              myRec = mySpreads[k].pages[0].rectangles.add(app.activeDocument.layers.item(-1));
                              myRec.properties = {
                                  geometricBounds: Rbounds,
                                  strokeWeight: 0,
                                  strokeColor: app.activeDocument.swatches.item("None")};   
                              myRec.place(File(myFiles[k]));
                              myRec.fit(FitOptions.PROPORTIONALLY);// to fit image in rectangle
                          }
                      }
                      

                       

                      rgds

                      • 8. Re: Need help in automating batch placing of images on spreads
                        Abigailrstahlman Level 1

                        Hi,

                         

                        Wow thank you both so much.

                         

                        I was having some layer issues in Ruq's script. The rectangles were being added to different layers, when I want them all added to the unlocked visable layer. Also, the rectangles being created on the locked/hidden layers were all different sizes.

                         

                        I did a lot of fussing around with

                         

                        myRectangle = myPage.rectangles.add(myDocument.layers.item(-1), undefined, undefined, {geometricBounds:[myY1, myX1, myY2, myX2], strokeWeight:0, strokeColor:myDocument.swatches.item("None")});

                         

                        But I had no luck.

                         

                        This problmen is resolved with Jump_Over's script. Of course now I am faced with another problem.

                         

                        My INDD (CS 5.5 on OSX 10.7.3) has an annoying sequencing glitch, it treats my numbers as strings, instead of integers. If I select eleven files to place manually, they will load into the cursor in order:

                         

                        myfile_

                        myfile_10

                        myfile_11

                        myfile_2

                        myfile_3

                        myfile_4

                        myfile_5

                        myfile_6

                        ...

                         

                        This same thing is happening when the script places images for me.

                         

                        Anyway around this issue?

                         

                        Thanks so much for all of the help, I really really appreciate it.

                        • 9. Re: Need help in automating batch placing of images on spreads
                          Jump_Over Level 5

                          Hi,

                           

                          include function:

                           

                          function smartSort (a,b) {

                              return Number(a.name.match(/\d+\./) ) > Number(b.name.match(/\d+\./) );

                              }

                           

                          if your files are collected inside myFiles array

                          use:

                           

                          myFiles.sort(smartSort);

                           

                          before placing files.

                          rgds

                          • 10. Re: Need help in automating batch placing of images on spreads
                            Abigailrstahlman Level 1

                            Hi Everyone,

                             

                            Here is our latest code, with smartSort added to line 27. Sadly we are still experiencing the same problem.

                             

                            For example, our files are sorted as:

                            123.jpg

                            123_10.jpg

                            123_11.jpg

                            123_2.jpg

                            123_3.jpg

                             

                            We're soooo close.  Any tips?

                             

                            function Init(){

                                    var myFilteredFiles;

                                    //Make certain that user interaction (display of dialogs, etc.) is turned on.

                                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                                    myExtensions = [".jpg", ".jpeg"]

                                    //Display the folder browser.

                                    var myFolder = Folder.selectDialog("Select the folder containing the images", "");

                                    //Get the path to the folder containing the files you want to place.

                                    if(myFolder != null){

                                                if(File.fs == "Macintosh"){

                                                    myFilteredFiles = myMacOSFileFilter(myFolder);

                                                }

                                                else{

                                                    myFilteredFiles = myWinOSFileFilter(myFolder);

                                                }

                                                if(myFilteredFiles.length != 0){

                                                        myPlaceImages(myFilteredFiles);

                                                        alert("Done!");

                                                }

                                          }

                                    }

                             

                             

                            // call the initial function

                            Init();

                             

                            function smartSort (a,b) {

                               //return Number(a.name.match(/\d+\./) ) > Number(b.name.match(/\d+\./) );

                               return Number(a.match(/\d+\./) ) > Number(b.match(/\d+\./) );

                            }

                             

                            //Windows version of the file filter.

                            function myWinOSFileFilter(myFolder){

                                      var myFiles = new Array;

                                      var myFilteredFiles = new Array;

                                      for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++){

                                                myExtension = myExtensions[myExtensionCounter];

                                                myFiles = myFolder.getFiles("*"+ myExtension);

                                                myFiles.sort(smartSort);

                                               

                                                if(myFiles.length != 0){

                                                          for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++){

                                                                  myFilteredFiles.push(myFiles[myFileCounter]);

                                                          }

                                                }

                                      }

                                      return myFilteredFiles;

                            }

                            function myMacOSFileFilter(myFolder){

                                      var myFilteredFiles = myFolder.getFiles(myFileFilter);

                                      return myFilteredFiles;

                            }

                            //Mac OS version of file filter

                            //Have to provide a separate version because not all Mac OS users use file extensions

                            //and/or file extensions are sometimes hidden by the Finder.

                            function myFileFilter(myFile){

                                      var myFileType = myFile.type;

                                      switch (myFileType){

                                                case "JPEG":

                                    return true;

                                                          break;

                                                default:

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

                                                          var myExtension = myExtensions[myCounter];

                                                          if(myFile.name.indexOf(myExtension)>-1){

                                                                    return true;

                                                                    break;

                                                          }

                                                }

                                      }

                                      return false;

                            }

                             

                            function myPlaceImages(myFiles){

                                var myCounter = myFiles.length;

                                app.activeDocument.documentPreferences.properties =

                                    {pagesPerDocument: myCounter*2,

                                        facingPages: true,

                                        allowPageShuffle: true};

                                app.activeDocument.pages[0].appliedSection.properties =

                                    {continueNumbering:false, pageNumberStart: 2};

                            //above is cause 1st spread can be 2-pages

                                var mySpreads = app.activeDocument.spreads;

                                var Pbounds = mySpreads[0].pages[1].bounds;

                            // each rectangle has same size: from [0,0] point to bottom right corner of spread

                                var Rbounds = [ 0, 0, Pbounds[2], Pbounds[3] ];

                                for (var k = 0; k < myCounter; k++) {

                                    myRec = mySpreads[k].pages[0].rectangles.add(app.activeDocument.layers.item(-1));

                                    myRec.properties = {

                                        geometricBounds: Rbounds,

                                        strokeWeight: 0,

                                        strokeColor: app.activeDocument.swatches.item("None")};  

                                    myRec.place(File(myFiles[k]));

                                    myRec.fit(FitOptions.PROPORTIONALLY);// to fit image in rectangle

                                }

                            }

                             

                            Thanks!

                            • 11. Re: Need help in automating batch placing of images on spreads
                              MrTIFF Level 3

                              Looks like you're mainly just missing the fact that string.match() returns an Array of matched substrings. So, smartSort could look something like this:

                               

                              function smartSort(a, b) {

                                        var aMatches = a.name.match(/\d+\./);

                                        var bMatches = b.name.match(/\d+\./);

                                        var aVal = (aMatches && aMatches.length > 0) ? aMatches[aMatches.length-1] : "0";

                                        var bVal = (bMatches && bMatches.length > 0) ? bMatches[bMatches.length-1] : "0";

                                        return Number(aVal) - Number(bVal);

                              }

                               

                              And you might want the sort() call to happen right before myPlaceImages()?

                              • 12. Re: Need help in automating batch placing of images on spreads
                                Abigailrstahlman Level 1

                                Got it working!!

                                 

                                Thanks so much everyone.

                                 

                                Here is the working script, in case anybody else ever needs it. Again I am in CS5.5 OSX 10.7.

                                 

                                function Init(){

                                        var myFilteredFiles;

                                        //Make certain that user interaction (display of dialogs, etc.) is turned on.

                                        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                                        myExtensions = [".jpg", ".jpeg"]

                                        //Display the folder browser.

                                        var myFolder = Folder.selectDialog("Select the folder containing the images", "");

                                        //Get the path to the folder containing the files you want to place.

                                        if(myFolder != null){

                                                    if(File.fs == "Macintosh"){

                                                        myFilteredFiles = myMacOSFileFilter(myFolder);

                                                    }

                                                    else{

                                                        myFilteredFiles = myWinOSFileFilter(myFolder);

                                                    }

                                                    if(myFilteredFiles.length != 0){

                                                            myFilteredFiles.sort(smartSort);

                                                            myPlaceImages(myFilteredFiles);

                                                            alert("Done!");

                                                    }

                                              }

                                        }

                                 

                                // call the initial function

                                Init();

                                 

                                function smartSort(a, b){

                                          var aMatches = a.name.match(/\d+\./);

                                          var bMatches = b.name.match(/\d+\./);

                                          var aVal = (aMatches && aMatches.length > 0) ? aMatches[aMatches.length-1] : "0";

                                          var bVal = (bMatches && bMatches.length > 0) ? bMatches[bMatches.length-1] : "0";

                                          return Number(aVal) - Number(bVal);

                                }

                                function myMacOSFileFilter(myFolder){

                                          var myFilteredFiles = myFolder.getFiles(myFileFilter);

                                          return myFilteredFiles;

                                }

                                //Windows version of the file filter.

                                function myWinOSFileFilter(myFolder){

                                    var myFiles = new Array;

                                    var myFilteredFiles = new Array;

                                    for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++){

                                        myExtension = myExtensions[myExtensionCounter];

                                        myFiles = myFolder.getFiles("*"+ myExtension);

                                        if(myFiles.length != 0){

                                            for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++){

                                                myFilteredFiles.push(myFiles[myFileCounter]);

                                            }

                                        }

                                    }   

                                    return myFilteredFiles;

                                }

                                //Mac OS version of file filter

                                //Have to provide a separate version because not all Mac OS users use file extensions

                                //and/or file extensions are sometimes hidden by the Finder.

                                 

                                function myFileFilter(myFile){

                                          var myFileType = myFile.type;

                                          switch (myFileType){

                                                    case "JPEG":

                                        return true;

                                                              break;

                                                    default:

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

                                                              var myExtension = myExtensions[myCounter];

                                                              if(myFile.name.indexOf(myExtension)>-1){

                                                                        return true;

                                                                        break;

                                                              }

                                                    }

                                          }

                                          return false;

                                }

                                function myPlaceImages(myFiles){

                                    var myCounter = myFiles.length;

                                    app.activeDocument.pages[0].appliedSection.properties =

                                        {continueNumbering:false, pageNumberStart: 2};

                                //above is cause 1st spread can be 2-pages

                                    var mySpreads = app.activeDocument.spreads;

                                    var Pbounds = mySpreads[0].pages[1].bounds;

                                // each rectangle has same size: from [0,0] point to bottom right corner of spread

                                    var Rbounds = [ 0, 0, Pbounds[2], Pbounds[3] ];

                                    for (var k = 0; k < myCounter; k++) {

                                        myRec = mySpreads[k].pages[0].rectangles.add(app.activeDocument.layers.item(-1));

                                        myRec.properties = {

                                            geometricBounds: Rbounds,

                                            strokeWeight: 0,

                                            strokeColor: app.activeDocument.swatches.item("None")}; 

                                        myRec.place(File(myFiles[k]));

                                        myRec.fit(FitOptions.PROPORTIONALLY);// to fit image in rectangle

                                    }

                                }

                                 

                                Thanks again!

                                • 13. Re: Need help in automating batch placing of images on spreads
                                  Colin Flashman Adobe Community Professional

                                  I can see another application of this script: placing spreads of a PDF so that the file can be re-PDFd as single pages instead. This would be a solution for print providers who have to break readers spreads into single pages. I've outlined a different indesign method before and a wholly PDF based solution here: https://colecandoo.wordpress.com/2013/01/13/when-readers-should-be-single-version-2-0/ BUT this method would be yet another failsafe.

                                   

                                  Apart from chaning the JPG filters in the script and changing the myRec.fit(FitOptions.PROPORTIONALLY) to myRec.fit(FitOptions.frameToContent), I'm a bit stuck how to change the start point of the image. In my circumstances the image would need to start from the centre of the spread and grow outwards. Figure the changes to the script would be something to do with the Rbounds... ideas?

                                  • 14. Re: Need help in automating batch placing of images on spreads
                                    Jump_Over Level 5

                                    Hi cdflash,

                                     

                                    If  a rectangle gBounds could stay equal to spread bounds, maybe

                                     

                                    myRec.fit(FitOptions.CENTER_CONTENT)

                                     

                                    would be good enough?

                                     

                                    and

                                    myRec.fit(FitOptions.FRAME_TO_CONTENT)

                                    in next step... or not.

                                     

                                    hope...

                                    • 15. Re: Need help in automating batch placing of images on spreads
                                      Colin Flashman Adobe Community Professional

                                      @Jump_Over

                                       

                                      that did the trick, thanks . Now have 3 solutions to fix readers spreads to single pages... awesome!