4 Replies Latest reply on Feb 18, 2014 9:48 AM by David André Erichsen

    Insert an image if it does not already exists on the page

    David André Erichsen

      I'm coding one if satement in a loop (go through all the pages). I want to place a picture on every page, but only if it does not already exist, for instance if the script is run before.

       

      Any suggestion on how to code this?

        • 1. Re: Insert an image if it does not already exists on the page
          Laubender Adobe Community Professional & MVP

          @David André – loop the allGraphics array of the page and check for:

           

          itemLink of the graphics ( = the source file)

           

          Note: checking the name of an image is not enough, since you can place the images with the same name, but different contents, from various folders in your file system.

           

          Uwe

          • 2. Re: Insert an image if it does not already exists on the page
            David André Erichsen Level 1

            I was hoping for a more concrete snippet

            • 3. Re: Insert an image if it does not already exists on the page
              Laubender Adobe Community Professional & MVP

              @David André – alright, but before, I suggest you have a deeper look at the DOM documentation (best download the chm file representaion):

               

              http://www.jongware.com/idjshelp.html

               

              http://jongware.mit.edu/idcs6js/pc_Page.html

              http://jongware.mit.edu/idcs6js/pc_Graphic.html

              http://jongware.mit.edu/idcs6js/pc_Link.html

               

              http://jongware.mit.edu/idcs6js/pc_File.html

               

              http://jongware.mit.edu/idcs6js/pc_Page.html#place

               

              Let's go and start with a selected image on a page (the one you like to place at least one time on every page).

               

              //SelectedGraphic_Place_IfNotOnPage_DOCUMENT.jsx
              //Uwe Laubender
              
              /**
              * @@@BUILDINFO@@@ SelectedGraphic_Place_IfNotOnPage_DOCUMENT.jsx !Version! Tue Feb 18 2014 09:54:55 GMT+0100
              */
              
              /*
                  See discussion at:
                  David André Erichsen
                  Insert an image if it does not already exists on the page
                  http://forums.adobe.com/thread/1409013?tstart=0
              */
              
              //FIRST DO SOME CHECKS ON THE SELECTION:
              
              if(app.selection.length === 0){
                  alert("Nothing selected!");
                  exit();
                  };
              
              if(app.selection.length > 1){
                  alert("Select ONE SINGLE graphic container only!");
                  exit();
                  };
              
              if(app.selection[0].graphics.length !== 1){
                  alert("Select a graphic container only!");
                  exit();
                  };
              
              if(app.selection[0].graphics[0].itemLink == null){
                  alert("Copy/Pasted images WITHOUT a link will not qualify as starting point!");
                  exit();
                  };
              
              var myDoc = app.documents[0];
              
              //The selection; in this example the container of the image [Superclass is Graphic]
              var myImageContainer = app.selection[0];
              
              //The image sitting in its container:
              var myImageAsGraphic = app.selection[0].graphics[0];
              
              //If the image is linked we have a valid itemLink [Class Link] and (hopefully) a valid filePath
              //File(filePath) constructs a File object:
              var myImageFile = File(app.selection[0].graphics[0].itemLink.filePath);
              
              //Check for existance of the File in your file system:
              if(!myImageFile.exists){
                  alert("No valid file found. Link for file: "+myImageFile.name+" is broken.");
                  exit();
                  };
              
              for(var n=0;n<myDoc.pages.length;n++){
                  
                  var allGraphicsOfCurrentPageArray = myDoc.pages[n].allGraphics;
                  var countMyFile = 0;
                  
                  for(var g=0;g<allGraphicsOfCurrentPageArray.length;g++){
                      
                      //THIS COULD BREAK, IF AN IMAGE IS NOT PLACED ON A PAGE, 
                      //BUT INSERTED WITH copy/paste AND HAS NO LINK!
                      
                      //See the following representations of the file path in the JavaScript Console
                      //Uncomment the lines for debugging:
                      
              //~         $.writeln(myImageFile);
              //~         $.writeln(allGraphicsOfCurrentPageArray[g].itemLink.filePath);
              //~         $.writeln(File(allGraphicsOfCurrentPageArray[g].itemLink.filePath));
              //~         $.writeln("*************");
                      
                      
                      //HOWEVER THE FOLLOWING WILL NOT WORK:
                      //Compare File with File with the line commented out below
                      //(Even if you use "==" instead of "===")
              
                      //if(myImageFile === File(allGraphicsOfCurrentPageArray[g].itemLink.filePath)){++countMyFile};
                      
                      //INSTEAD 
                      //COMPARE the absoluteURI of the both Files
                      //ALSO CHECK, if the itemLink is NOT null (that's the case, if the image is copy/pasted on the page)
                      
                      if(
                          allGraphicsOfCurrentPageArray[g].itemLink != null 
                          && myImageFile.absoluteURI === File(allGraphicsOfCurrentPageArray[g].itemLink.filePath).absoluteURI
                          )
                          {
                          ++countMyFile;
                          };
                      
                      };
                  
                  if(countMyFile === 0){
                      myDoc.pages[n].place(myImageFile);
                      };
                  
                  };
              
              exit();
              

               

               

              Uwe