5

I am writing some JS code to relink an image, then resize it to fit the containing object. Simplified version of code:

var image = (get image);
try {
  image.itemLink.relink(File(new_filename));
}
catch(e) {
  (log it);
}

var image = (find image again because after the relink it would otherwise cause error "Object no longer exists")

(work out new width, height, v offset, h offset);

try {
  if(image.locked) {
    lock_later = true;
    image.locked = false;
  }
}
catch(e) { }

// Resize and reposition image
image.geometricBounds = [(rectangle.geometricBounds[0] + h_offset) + "mm", (rectangle.geometricBounds[1] + w_offset) + "mm", (rectangle.geometricBounds[2] - h_offset) + "mm", (rectangle.geometricBounds[3] - w_offset) + "mm"];

// Lock the image again if it was locked before
if(lock_later) {
  image.locked = true;
}

With the try/catch block around the if(image.locked) block, the resize line throws the error "Image is locked" (because it fails to unlock it). Without the try/catch but keeping the if(image.locked) block, it throws the error "The property is not applicable in the current state." when trying to access image.locked.

So what "state" is my image in, and why is it not "applicable" even though the app is clearly using it to prevent me resizing it? How do I resize my image, given that this is an automated process and in production I won't have access to InDesign to edit it manually beforehand?

p.g.l.hall
  • 1,961
  • 2
  • 15
  • 26
  • What type of object is `image`? I believe that in the code not shown you are alternating between the container (which can be locked and unlocked) and the actual image inside (which cannot). Usually one locks the *container* page item, not the graphic object 'inside' it. – Jongware Oct 03 '14 at 08:38
  • 1
    The `image` variable is an `Image` object. According to the IDS docs, `locked` is a read/write boolean property on `Image` objects specifying whether the image is locked. I believe I can access the container item with `image.parent` - perhaps I can try unlocking that instead and seeing what happens? – p.g.l.hall Oct 03 '14 at 08:50
  • Paint me surprised. My main work horse is CS4 and so I just checked for that version, and you cannot lock a graphic. But: starting from the next version you *can*. I don't know if locking or unlocking a container also modifies its contents. It seems the documentation is unclear on that as well. – Jongware Oct 03 '14 at 08:56
  • Guess what? If I use `image.parent.locked` instead of `image.locked` it works fine. Re-post your comment as an answer and I will happily accept it and give you your bounty. Thanks :) – p.g.l.hall Oct 03 '14 at 09:40

1 Answers1

5

As stated in Adobe's documentation, an image container -- the parent frame 'around' an image, which is a generic SplineItem -- can be locked and unlocked by changing the read/write boolean property locked.

In InDesign CS4 and earlier, the Graphic Class did not have this property, but since InDesign CS5 onwards, the property locked also appears in there and in all of its derived classes. According to Adobe's documentation it is a read/write property. However, this appears to be wrong. Experimenting with CS6, I found the locked property of a graphic inside its parent frame only reflects the parent's state, and is in fact read-only.

In the InDesign user interface in CS4 and earlier, the menu item "Lock" is disabled when a graphic inside a frame is selected. In the user interface from CS5 and later, a locked item cannot be selected, so the menu item cannot be invoked.

The easiest workaround, given a handle to a graphic image, is to check and/or change the state through its parent:

image = app.activeDocument.allGraphics[0]; // a handle to the first graphic
image.parent.locked = false; // unlock it
Jongware
  • 22,200
  • 8
  • 54
  • 100