Javacript locking embeded image
In Illustrator 29.1 this script worked just fine. I place an image, move it, name it, lock it and embed it. But in 29.3 this code gives an error message: It says that it cannot operate on a locked layer, BUT it still locks the placed item. If I move the lock to after the embed, the lock does not happen. So this code no longer works in 29.3.
I think its weird in 29.1 that you were allowed to lock the placeditem before you embed it, but it doesn't work if you lock it afterwards.
We are using macos 15.1.1 (and 15.3.1). I have verified that this code functions on 29.1 and fails on 29.3. We have verified this across multiple desktop computers.
We found this issue in a much larger code base, but I was able to extract this down to this code chunk below.
var doc = app.activeDocument;
var scriptFile = new File($.fileName);
var scriptFolder = scriptFile.path;
var filePath = new File(scriptFolder + "/Golf Ball.png");
// Place the image
var placedItem = doc.placedItems.add();
placedItem.file = filePath;
// Center the image
var docBounds = doc.artboards[0].artboardRect;
var centerX = (docBounds[0] + docBounds[2]) / 2;
var centerY = (docBounds[1] + docBounds[3]) / 2;
placedItem.position = [centerX - placedItem.width / 2, centerY + placedItem.height / 2];
// Embed the image
placedItem.name = "Golf Ball Texture"; // Name the placed item
placedItem.locked = true;
placedItem.embed();

-
Sergey Osokin commented
That's not quite right. After embed(), automatically selected objects appear in the document. They are the app.selection collection. Since you have one object in the example, we access it by index [0]. If Illustrator did not automatically select these objects, it would be more difficult to detect them through JS code.
-
Eric Fox commented
Sergey,
Thanks so much! Solution 2 worked like a charm. I had thought maybe the embed created a new object but I didn't think about it returning an array of objects. Totally missed that.
-
Sergey Osokin commented
Confirm system alert after locking an object in 29.2 Mac OS. But I would make a safe version of the code for all Adobe versions.
Solution 1)
try {
placedItem.locked = true;
placedItem.embed();
} catch (err) {} // try...catch will skip alert in case of an errorSolution 2)
placedItem.embed();
placedItem = app.selection[0]; // Because a new object is created after embedding, so you need to reassign the variable
placedItem.locked = true;