11 Replies Latest reply on May 7, 2015 3:54 AM by BSKTCreation

    Automatically importing named images

    nathanm123

      Hi, I'm looking for a way in InDesign CS6 to import pdfs automatically into an image box based on criteria defined in a linked text box. We have a program that automatically creates a sized photo box  with a grouped text box on an InDesign page. The text box has categories like name and file reference number. If the pdfs could be saved with either the same file name or file reference number, is there a way to automatically pull them into the image box based on the file name?

        • 1. Re: Automatically importing named images
          TᴀW Adobe Community Professional & MVP

          Yes, it could be scripted.

          • 2. Re: Automatically importing named images
            BSKTCreation Level 2

            Hi Nathan,

             

            If I understand what you're doing the following should do what you describe.

             

            // First we need to change the DOM to a script label compatible version
            var originalDomVersion = app.scriptPreferences.version;
            app.scriptPreferences.version = "6.0";
            
            
            // Get PDFs
            var myPDFFolder = Folder("/H/My PDF Folder"); // The folder where the PDFs are stored
            var myPDFs = myPDFFolder.getFiles("*.pdf"); // Just getting PDF files here
            
            
            // Iterate throught the PDFs
            for (var i = 0; i < myPDFs.length; i++) {
                var myPageItems = app.activeDocument.pageItems; // Define all the page items in the document.
                // Iterate thought the page items
                for (var j = 0; j < myPageItems.length; j++) {
                    var myPDFNameTemp = String(myPDFs[i]).substring(21,); // Remove the path from the PDF variable, this number is the number of character to remove from before the file name so it would differ depending on the directory path in line 7
                    var myPDFName = myPDFNameTemp.substring(0, myPDFNameTemp.length - 4); // Remove the file extension from the temp variable
                    // The PDF will import if the file name (no path or extension) equals the script label of the page item, e.g. 1.PDF goes into app.activeDocument.pageItems.item("1");
                    if (app.activeDocument.pageItems[j].label === myPDFName) {
                        app.activeDocument.pageItems[j].place(myPDFs[i]);
                        }
                    }
                }
            
            
            // Lastly we need to revert the DOM to the original version
            app.scriptPreferences.version = originalDomVersion;
            
            
            
            
            
            
            
            
            

             

            Let me know if this is what you're after.

             

            Brett

            • 3. Re: Automatically importing named images
              Obi-wan Kenobi Adobe Community Professional

              Why not create directly a text box with the file to be imported name [script 1], launch a script 2 that converts the text box to an image box and imports the good file in it?

               

              The only thing to do after is saying: GOAL! 

              • 4. Re: Automatically importing named images
                BSKTCreation Level 2

                Nice suggestion Obi-wan, but I was expecting you to say "This isn't the script you're looking for..."

                • 5. Re: Automatically importing named images
                  Obi-wan Kenobi Adobe Community Professional

                  Take a look to:

                   

                  Place images

                   

                  A very clever script written by Kasyan Servetsky 5 years ago! 

                  • 6. Re: Automatically importing named images
                    nathanm123 Level 1

                    Hi BSKTCreation,

                    Thanks for your quick response. I tried out the script you suggested and it looks promising. There are a couple things I'm kind of getting caught up on that I was hoping you could point me in the right direction on.

                     

                    When I run the script on the grouped text and image boxes, it errors on the line "app.activeDocument.pageItems[j].place(myPDFs[i]);" and won't place any of the images.

                     

                    If I ungroup the boxes, I can get one of the images to pull in. But it doesn't replace the box. The same image pulls into both the text box and the image box. And the same image pulls in to the other boxes as well. In this case, the same image pulls into all six boxes.

                     

                    Is there a way to only pull the pdf into the background linked box and keep the text box in front?  Is there a way to pull the pdfs into the box based on any text in the box like the name or job number (i.e. if the pdfs are named "Name 2", "Name 3" or "142130A" and "Name1" or "141988")?

                     

                    Box sample.png

                    • 7. Re: Automatically importing named images
                      BSKTCreation Level 2

                      Hi Nathan,

                       

                      Yes you can do what you're after. The difference in what I did and what you need is my script is looking at the frame's script label where as your template is looking at it's content.

                       

                      So on the positive we don't need to play with the DOM version so that part of my script can go right away. On the negative the detail you are looking at makes the script a little more complex as you now need to splice up the frame content so you can get the detail and act upon those conditions.

                       

                      I'm not in the position right now to prototype a version that does it but in a few days I should be able to give it a go if you still need the help.

                       

                      Let me know.

                       

                      Brett

                      • 8. Re: Automatically importing named images
                        nathanm123 Level 1

                        Hi Brett,

                        Any help you could provide would be awesome. There is no rush. I will try a couple things on it also and let you know on any progress I make.

                        Thanks again,

                        Nathan

                        • 9. Re: Automatically importing named images
                          BSKTCreation Level 2

                          Hi Nathan,

                           

                          I managed to get a little time to write a quick function.

                           

                          Please note that this only works on the file name (not the job numbers), this is because from my understanding the job number won't be in the file name so I don't know how you'd be identifying the PDF other than it's name (maybe it's metadata?).

                           

                          (function() {
                          
                              var myPDFFolder = Folder("/H/My PDF Folder"); // Change this to your directory
                              var myPDF = myPDFFolder.getFiles("*.pdf"); // Could do any importable file type, change here
                              var myTotalPDFs = myPDF.length;
                              var myPage = app.activeDocument.pages;
                              var myTotalPages = myPage.length;
                          
                              // Iterate through each page in the document.
                              for (var i = 0; i < myTotalPages; i++) {
                                  var myTextFrame = myPage[i].textFrames;
                                  var myTotalTextFrames = myTextFrame.length;
                               
                                  // Check all textFrames on the page and get it's location.
                                  for (var j = 0; j < myTotalTextFrames; j++) {
                                      var myTextFrameLocation = myTextFrame[j].geometricBounds;
                                      var myRectangle = myPage[i].rectangles;
                                      var myTotalRectangles = myRectangle.length;
                                   
                                      // Look at all rectangles on the page and get it's location.
                                      for (var k = 0; k < myTotalRectangles; k++) {
                                          var myRectangleLocation = myRectangle[k].geometricBounds;
                                       
                                          // Compare the textFrame location to each rectangle location.
                                          if (
                                          myTextFrameLocation[0] >= myRectangleLocation[0] &&
                                          myTextFrameLocation[1] >= myRectangleLocation[1] &&
                                          myTextFrameLocation[2] <= myRectangleLocation[2] &&
                                          myTextFrameLocation[3] <= myRectangleLocation[3]
                                          ) {
                                           
                                              for (var l = 0; l < myTotalPDFs; l++) {
                                                  // If the locations overlap then get the PDF file name based on the textFrame first line contents.
                                                  var myPDFName;
                                                  if (myTextFrame[j].lines.length === 1) {
                                                      myPDFName = myTextFrame[j].contents;
                                                      }
                                                  else {
                                                      myPDFNameTemp = myTextFrame[j].lines[0].contents;
                                                      myPDFName = myPDFNameTemp.substring(0, myPDFNameTemp.length - 1);
                                                      }
                                               
                                                  // Put the file name and path into a variable.
                                                  var myPDFNameIncPath = myPDFFolder + "/" + myPDFName + ".pdf";
                                               
                                                  // Check the PDF name matches the contents and place it.
                                                  if (myPDFNameIncPath === String(myPDF[l])) {
                                                      myRectangle[k].place(myPDF[l]);
                                                      }
                                                  }
                                              }
                                          }
                                      }
                                  }
                              })();
                          
                          
                          
                          
                          

                           

                          It's 4 nested loops which could definitely be improved upon however it's quick and does work. I'm sure the community would find a more compact/better way to do this but I've always been a 'first get it working then re-factor later' type of programmer.

                           

                          Let me know how you go.

                           

                          Brett

                          • 10. Re: Automatically importing named images
                            nathanm123 Level 1

                            Hi Brett,

                            I tried out the script you put together and it looks great. It does pull in images based on their exact name, which is awesome. It looks like it may be possible to also pull in the file either by name or job number. We would just need to define the variable as either one. The name will always be on the first line and the job number always on the fifth in a format like this.

                             

                            Name: Name

                            Width: 5 in

                            Depth: 20 in

                            Color: Black

                            Job Number: 142094

                             

                            So it should be fairly easy to illuminate "Name: " and "Job Number: " from the variable like we do with the extension.

                             

                            One thing though, I noticed that the script won't pull in the file name if it has spaces in the name or if the name is just a number. Do you know if there is a way to include multiple words or numbers in the variable we define?

                             

                            Thanks again. You rock.

                            Nathan

                            • 11. Re: Automatically importing named images
                              BSKTCreation Level 2

                              Hi Nathan,

                               

                              To get the files to load with spaces you can escape the name.

                               

                              So line 44 in the function would change to:

                              var myPDFNameIncPath = myPDFFolder + "/" + escape(myPDFName) + ".pdf";
                              

                               

                              You are correct in regards to the other details in the textFrame. You should have enough info to extrapolate lines 35 to 41 to include those items.

                               

                              Brett