2 Replies Latest reply on Jan 1, 2013 9:08 PM by BEGINNER_X

    Get image file name

    BEGINNER_X Level 3

      Hi ALL,



      I want all image file naming like a caption in my source document. Source document contains more than 100 images.


      But i got a script through the forum and tuned for my script. Still, the file naming comes for the Ist page image only.


      From second page onwards the file naming not comes like a caption.



      Trying script:


      //To Get image file name


      var tgt = app.activeDocument.rectangles;


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


          myCaption = app.activeDocument.textFrames.add();


          myCaption.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN


          myCaption.contents = tgt[i].graphics[0].itemLink.name


          myCaption.paragraphs[0].justification = Justification.CENTER_ALIGN;


          bnds = tgt[i].visibleBounds;


          myCaption.visibleBounds =


              [bnds[2]+12, bnds[1], bnds[2]+4,bnds[3]];


              //[bnds[0]-6, bnds[1], bnds[0]-1,bnds[3]];







      Screen Shot 2012-12-31 at 11.39.42 PM.png



      Thanks in advance


        • 1. Re: Get image file name
          Laubender Adobe Community Professional & MVP

          If you use the add() method in conjunction with the document without specifying the page or better the spread, all objects added will end up on the first spread of your document.


          But your testing scenario is also too narrow. You are expecting:


          1. All your graphics are sitting in Rectangle objects (what about Ovals or Polygons?)

          Therefore: All graphics placed in ovals or polygons are left out.


          2. That really ALL rectangles in your document contain graphics

          You are in trouble, if there happen to be empty rectangles on a page. I would suggest to test that with your script. It will give you an error, because there is no graphics[0] for this specific rectangle, where you can draw the "name" for the "itemLink".


          3. That there are no graphics in anchored objects or pasted inside other objects; left alone graphics inside table cells.


          And who can say that thiese three restrictions exist in all documents you want to run this script against?


          So, if you want to get to all image files in the document, why don't you start with the images in the first place?


          Let's see:

          the Document object has an "allGraphics" property.

          Use that as a starting point and you will not missing a single graphic.*


          Iterate through "allGraphics" will get you:


          the "name" of the placed image through its itemLink.name property;
          the container of the graphic, that's the "parent" of the graphic.


          Now you need one other thing:
          the page, or even better(!) the spread where the graphic is located. Imagine a graphic sitting outside of a page.
          If you want to handle thiese, you must know the spread.


          If  writing this script for InDesign CS5 or above, you are in luck. There is a "parentPage" property of the "Graphic" object, you could use for your add() method. For graphics outside of pages this property will be "null".


          And another important thing:

          if a graphic is copy/pasted from e.g. PhotoShop to InDesign, the "itemLink"  property of that graphic is also "null".

          We can handle that in a try/catch scenario…



          //Scope: all graphics on all pages
          //(that includes graphics of anchored objects, active states of MSOs, graphics in table cells as well)
          var tgt = app.activeDocument.allGraphics;
          for(var i=0;i<tgt.length;i++){
              //Narrow the scope to all graphics on pages only:
              if(tgt[i].parentPage != null){
                  //What to do with graphics, that are pasted directly from none-InDesign files:
                  //the caption will read "Undefined"
                  var myName = tgt[i].itemLink.name;
                  }catch(e){var myName = "Undefined"};
                  //targets the page of the graphic:
                  var myPage = tgt[i].parentPage;
                  //adds a text frame to the page of the graphic:
                  var myCaption = myPage.textFrames.add();
                  myCaption.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
                  myCaption.contents = myName;
                  myCaption.paragraphs[0].justification = Justification.CENTER_ALIGN;
                  //Why visible bounds and not geometric bounds?
                  var bnds = tgt[i].parent.visibleBounds;
                  myCaption.visibleBounds = [bnds[2]+12, bnds[1], bnds[2]+4,bnds[3]];



          *Not exactly true, because it is missing graphics in not-active states of  MultiStateObjects.

          Since MSOs were introduced in InDesign CS5 the DOM documentation left out this fact so far.


          //EDIT: in the first version I had a comment on a different scope. Since  Document Objects have no "graphic" property, I removed that comment. Sorry.




          P.S. And a Happy New Year to all!


          Message was edited by: Laubender

          1 person found this helpful
          • 2. Re: Get image file name
            BEGINNER_X Level 3

            Hi Laubender,


            Thanks for your reply.....


            Thanks for your brief answer and your script.


            Its realy good for lot of beginners.