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?
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