5 Replies Latest reply on Jan 18, 2011 11:46 AM by Haakenlid

    Indesign CS4- How can I set the file to pull in images through out the file?

    nicshe

      I have a very large file/book.  There are many pictures that need to go into specific points in the  file. They can be inline graphics. Is there anyway to automate the file  so that it would pull in the pictures at their specific locations using  their file names?

       

      For instance, on page one I have text  and then the image name is smallrock.jpg. Can the file automatically  find that file name, pull it in and place it where the file name appears  in the text in the Indesign file?

       

      All the images are in one folder.

       

      Thank you for any help.

        • 1. Re: Indesign CS4- How can I set the file to pull in images through out the file?
          Haakenlid Level 3

          This will do it.

           

          Probably need some tweaking, though.

           

          It will not find filenames with spaces in them, for instance, and will not remove whitespace around the matches after replacing the filename with a placed image.

           

           

          var myDocument = app.activeDocument;
          var myFolder = Folder.selectDialog ("Where are the images?");
          var lookFor = "(?i)\\b\\S+\\.(jpg|tif|png)\\b"; // add more file extensions if you need.
          var myFiles = myFolder.getFiles(); //
          var myMatches = [];
          var myReport = "Placing images from "+myFolder.fullName+".";
          var myImage;
          var imageSize = {height: 5, width: 5}; // Size of placed images
          var myGB;
          var placedImageStyle = "Anchored Image"; // Name of Object style for placed images
          
          placedImageStyle = myDocument.objectStyles.itemByName (placedImageStyle) == null ? myDocument.objectStyles.add ({name: placedImageStyle}): myDocument.objectStyles.itemByName (placedImageStyle)
          
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences.findWhat = lookFor;
          myMatches = myDocument.findGrep();
          
          matchLoop:
          for (var n = myMatches.length-1; n>=0; n--){
               for (var m = myFiles.length-1; m>=0; m--){
                    if (myMatches[n].contents==myFiles[m].name){
                         myImage = myMatches[n].insertionPoints[-1].rectangles.add();
                         myMatches[n].contents="";
                         myImage.contentType = ContentType.graphicType;
                         myImage.appliedObjectStyle = placedImageStyle;
                         myImage.place (myFiles[m]);
                         myGB = myImage.geometricBounds;
                         myGB = [myGB[0], myGB[1], myGB[0]+imageSize.height, myGB[1]+imageSize.width];
                         myImage.geometricBounds = myGB;
                         myImage.fit(FitOptions.FILL_PROPORTIONALLY);
                         myReport = myReport + "\n\n"+myFiles[m].name+"\tOK";
                         continue matchLoop;
                    } 
               }
               myReport = myReport + "\n\n"+myMatches[n].contents+"\tNOT FOUND";
          }
          
          alert (myReport);
          
          • 2. Re: Indesign CS4- How can I set the file to pull in images through out the file?
            nicshe Level 1

            Thank you very much. I don't know how to work with scripts. Could you please direct me to some information. I was advised to post my question here, but I have no idea what to do with the script.

             

            Thank you again.

            • 3. Re: Indesign CS4- How can I set the file to pull in images through out the file?
              Haakenlid Level 3

              http://indesignsecrets.com/how-to-install-scripts-in-indesign.php

               

              Paste the code I wrote into a plain text editor and save with jsx. extension.

              • 4. Re: Indesign CS4- How can I set the file to pull in images through out the file?
                nicshe Level 1

                Thank you. I did it, but it doesn't get past the part were it is asking me for a folder. I also notice that you have "Name of Object Style." I don't have any object styles on I am just placing the images inline with the text.

                 

                For example,

                 

                text. text. text. text. text.

                text. text. text. text. text.

                [name of image.pdf]          - Can your code match this name with the name in the folder and place the image right in that space?

                text. text. text. text. text.

                text. text. text. text. text.

                 

                 

                 

                text. text. text. text. text.

                text. text. text. text. text.

                 

                IMAGE PLACED HERE.

                 

                text. text. text. text. text.

                text. text. text. text. text.

                 

                 

                Thank you again for your time and help!

                • 5. Re: Indesign CS4- How can I set the file to pull in images through out the file?
                  Haakenlid Level 3
                  Thank you. I did it, but it doesn't get past the part were it is asking me for a folder. I also notice that you have "Name of Object Style." I don't have any object styles on I am just placing the images inline with the text.

                   

                  What happened? Did you get any error massages?

                   

                  The script will make an object style of that name if you don't have it already. Object Styles will make it much easier to style the imported files, apply text wrap and so on.

                   

                  You can also write "[Basic Graphics Frame]" if you really don't want a style.

                   

                  To match filenames like [name of image.pdf] I had to do some small changes:

                   


                  var myDocument = app.activeDocument;
                  var myFolder = Folder.selectDialog ("Where are the images?");
                  var lookFor = "(?i)\\[[^.]+\\.(jpg|tif|png|pdf)\\]"; // add more file extensions if you need.
                  var myFiles = myFolder.getFiles();
                  var fileName = "";
                  var myMatches = [];
                  var myReport = "Placing images from "+myFolder.fullName+".";
                  var myImage;
                  var imageSize = {height: 5, width: 5}; // Size of placed images
                  var myGB;
                  var placedImageStyle = "Anchored Image"; // Name of Object style for placed images

                  placedImageStyle = myDocument.objectStyles.itemByName (placedImageStyle) == null ? myDocument.objectStyles.add ({name: placedImageStyle}): myDocument.objectStyles.itemByName (placedImageStyle)

                  app.findGrepPreferences = NothingEnum.nothing;
                  app.changeGrepPreferences = NothingEnum.nothing;
                  app.findGrepPreferences.findWhat = lookFor;
                  myMatches = myDocument.findGrep();

                  matchLoop:
                  for (var n = myMatches.length-1; n>=0; n--){
                        fileName = myMatches[n].contents.slice(1,-1);
                       for (var m = myFiles.length-1; m>=0; m--){
                            if (fileName==myFiles[m].name){
                                 myImage = myMatches[n].insertionPoints[-1].rectangles.add();
                                 myMatches[n].contents="";
                                 myImage.contentType = ContentType.graphicType;
                                 myImage.appliedObjectStyle = placedImageStyle;
                                 myImage.place (myFiles[m]);
                                 myGB = myImage.geometricBounds;
                                 myGB = [myGB[0], myGB[1], myGB[0]+imageSize.height, myGB[1]+imageSize.width];
                                 myImage.geometricBounds = myGB;
                                 myImage.fit(FitOptions.FILL_PROPORTIONALLY);
                                 myReport = myReport + "\n\n"+myFiles[m].name+"\tOK";
                                 continue matchLoop;
                            }
                       }
                       myReport = myReport + "\n\n"+fileName+"\tNOT FOUND";
                  }

                  alert (myReport);