Skip navigation
Currently Being Moderated

Unlocking Locked Layers

Dec 7, 2009 6:06 PM

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.

 
Replies
  • Currently Being Moderated
    Dec 7, 2009 10:36 PM   in reply to Anurag Mendhekar

    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;

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 8, 2009 2:31 AM   in reply to Anurag Mendhekar

    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;

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points