0

I am intermediate with Javascript, but am not so familiar with Adobe's "Extendscript". For practice and a better understanding of InDesign's code structure, I am trying to access properties of an image via rectangles.images.

Is this possible to access say, the file name of an image, through rectangles.images? Also I am interested if it's possible to access the image's color attributes this way, say to convert it to greyscale?

Here is my approach so far:

for(var i = 0; i < app.activeDocument.rectangles.length; i++)
{
    var imageType = app.activeDocument.rectangles[i].images.constructor.name;

    switch(imageType)
    {
        case "Images":
            alert(app.activeDocument.rectangles[i].images.name); // "name" is not a valid property here!
            break;

        default:
            alert("There are no images in this file.");
    }
}

Also, is it possible to determine the file-type of the image with .rectangles.images.constructor.name? I would like to add an extra case for say, PDF's or jpegs.

Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

1 Answers1

3

You shouldnt use the constructor unless you want to try and determine what type of JS Object it is which you dont need to do in this case because the images collection is only going to contain images. The file property will actually be on an image's related Link object.

Note none of this is tested i jsut took my knowledge of JS and the API documentation and reworked your code...

var rect = app.activeDocument.rectangles,
    imgs,
    filePath,
    hasImages = false;

for(var i = 0; i < rect.length; i++) {
    imgs = rect[i].images;
    if( imgs.length > 0) {

      hasImages = true;
      for (var j = 0; j < imgs.length; j++) {
         filePath = imgs[j].itemLink ? imgs[j].itemLink.filePath : null; 
         switch (imgs[j].imageTypeName) {
             case 'jpeg':
                alert('This is a JPEG:' + filePath);
                break;
             case 'pdf':
                alert('This is a PDF: '+filePath);
                break;
             default:
               alert('Default case - '+imgs[j].imageTypeName+': '+filePath);
         }
      }
   }
} 

if(!hasImages) {
   alert('No images in document');
}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • Thanks @prodigitalson! I was not aware of the `itemLink` and `filePath` properties until you had answered this question. – Ian Campbell Jun 29 '12 at 06:35
  • I got the file from another source , and its give me file path of that system, but when I export it into html , it gives me the image in separate folder, mean image are also encoded /attached into file – Muhammad Ali Oct 13 '16 at 10:37