I'm trying to unlock layers in a document through Javascript (CS3/4).
function clearAllLocks(doc){
var layers=doc.layers;
for(var i=0;i<layers.length;i++){
if (layers[i].locked){
layers[i].locked=false;
}
}
}
This function runs fine (no errors reported). Here's the problem: This does nothing. All subsequent operations on the layers result in a "layer cannot be modified" error. Is there something that I'm missing?
I've noticed the problem even if I manually unlock the layer. The only thing that works is to manually do the following:
a. unlock the layer
b. duplicate it
c. delete the old layer.
d. rename duplicate layer to the old one
Any help from the gurus will be highly appreciated.
A.
the following script does those steps(or it should do, as i dun have to test it now):
how it works: select an item from the current document that's in the layer that you want to use as the original layer:
var doc = app.activeDocument;
clearAllLocks(doc);
function clearAllLocks(doc){
var layers=doc.layers;
for(var i=0;i<layers.length;i++){
if (layers[i].locked){
layers[i].locked=false;
}
}
}
var sel = doc.selection;
var layer = sel[0].parent;
var lyrName = layer.name;
var nL = layer.duplicate();
nL.name = lyrName;
layer.remove();
hope it helps;
cheers;
Thanks Sonic, but that doesn't work because layer does not have a duplicate method.
Anyhow, more digging has revealed that the error arises due to a very subtle issue that has little to do with locking. What's happening is that the script is failing on documents that have layers that are both locked AND hidden (turns out all the files I was dealing with were like this). So, when I moved an item from a hidden layer to a non-hidden one, I saw this error. I mistakenly interpreted this to mean that the locking was the problem, when it really wasn't. I found the issue out purely by chance. It is the error message that is broken and needs fixing.
I'm not quite sure why it worked with the manual process, but I'm guessing that's the real bug :-)
Thanks for your effort.
A.
this should work with whatever you need ... since you weren't very precise about your problem i can only confirm that:
- operations that are made on a locked or non visible layer cause errors on Windows machines and cause program crash on Mac machines(took me half an hour to figure out that lol)
var doc = app.activeDocument;
clearLayerOverrides(doc);
function clearLayerOverrides(doc){
var layers=doc.layers;
for(var i=0;i<layers.length;i++){
if (!layers[i].visible){
layers[i].visible=true;
}
if (layers[i].locked){
layers[i].locked=false;
}
}
}
function moveItems(fromLayer, toLayer){
for(j = fromLayer.pageItems.length - 1; j >= 0; j--) {
fromLayer.pageItems[j].duplicate(toLayer);
}
}
var sel = doc.selection;
var layer = sel[0].parent;
var lyrName = layer.name;
var nL = doc.layers.add();
nL.name = lyrName;
moveItems(layer,nL);
layer.remove();
hope it helps;
cheers;
North America
Europe, Middle East and Africa
Asia Pacific