Skip to main content
September 27, 2010
Question

Unlocking Background Layer with lots of other layers

  • September 27, 2010
  • 3 replies
  • 10385 views

I've been looking at the similar questions, but no script has worked perfectly yet. I have a series of phoshop actions that I run on a regular basis. I have two version of each, one for if there's a locked background layer, and one for when its already been unlocked. Otherwise my actions error.

I'd like to simplify the process and have a script run that finds any locked layers (ie: background) and unlocks it, even if there are 15 other layers and even if I have another layer selected. It should also run without errors if there are no locked (background) layers to be found.

I don't know the first thing about writing my own scripts, so can somebody tell me what lines of code I'd need? Thank you Thank you!

This topic has been closed for replies.

3 replies

March 6, 2018

How do I copy this script? text edit and save as a .jsx?

Inspiring
September 27, 2010

Just a couple quick questions:

1. Did you want ALL locked layers in the stack to be unlocked or just the bottom layer on the stack?

2. When you start your action, which layer is active? Or is it random?

September 27, 2010

1) I want all Locked layers (99% of the time this is just the background layer) to be unlocked

2) Random

(thank you for looking into it!)

Inspiring
November 5, 2013

what about making the invisible layer visible/active, then check if it's locked, then unlock and turn off visiblity again?


function makeActiveByIndex( index, visible ){

    var desc = new ActionDescriptor();

    var ref = new ActionReference();

    ref.putIndex(charIDToTypeID( 'Lyr ' ), index )

    desc.putReference( charIDToTypeID( 'null' ), ref );

    desc.putBoolean( charIDToTypeID( 'MkVs' ), visible );

    executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );   

};

function getNumberOfLayers(){

    var ref = new ActionReference();

    ref.putProperty( charIDToTypeID( 'Prpr' ), charIDToTypeID( 'NmbL' ) );

    ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID( 'Ordn' ), charIDToTypeID( 'Trgt' ) );

    return executeActionGet( ref ).getInteger( charIDToTypeID( 'NmbL' )) ;

};

function getProperty( psClass, psKey, index ){// integer:Class, integer:key

    var ref = new ActionReference();

    if( psKey != undefined ) ref.putProperty( charIDToTypeID( "Prpr" ), psKey );

    if(index != undefined ){

        ref.putIndex( psClass, index );

    }else{

        ref.putEnumerated( psClass , charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

    }

    try{

        var desc = executeActionGet(ref);

    }catch(e){ return; }// return on error

    if(desc.count == 0) return;// return undefined if property doesn't exists

    var dataType = desc.getType(psKey);

    switch(dataType){// not all types supported - returns undefined if not supported

        case DescValueType.INTEGERTYPE:

            return desc.getInteger(psKey);

            break;

        case DescValueType.ALIASTYPE:

            return desc.getPath(psKey);

            break;

        case DescValueType.BOOLEANTYPE:

            return desc.getBoolean(psKey);

            break;

        case DescValueType.BOOLEANTYPE:

            return desc.getBoolean(psKey);

            break;

        case DescValueType.UNITDOUBLE:

            return desc.getUnitDoubleValue(psKey);

            break;

        case DescValueType.STRINGTYPE:

            return desc.getString(psKey);

            break;

        case  DescValueType.OBJECTTYPE:

            return desc.getObjectValue(psKey);

            break;

        case  DescValueType.LISTTYPE:

            return desc.getList(psKey);

            break;

        case  DescValueType.ENUMERATEDTYPE:

            return desc.getEnumerationValue(psKey);

            break;

    }

};

var doc = app.activeDocument;

var layerCount = getNumberOfLayers();

var invisibleLayers = [];

var loopStart = Number(!doc.layers[doc.layers.length-1].isBackgroundLayer);

for(var layerIndex = loopStart;layerIndex<=layerCount;layerIndex++){

    if(!getProperty( charIDToTypeID('Lyr '), stringIDToTypeID( 'visible' ), layerIndex )) invisibleLayers.push(layerIndex);

}

for(var hiddenIndex=0;hiddenIndex<invisibleLayers.length;hiddenIndex++){

    makeActiveByIndex( invisibleLayers[hiddenIndex], true);

    if(doc.activeLayer.allLocked) doc.activeLayer.allLocked = false;

    if(doc.activeLayer.pixelsLocked) doc.activeLayer.pixelsLocked = false;

    if(doc.activeLayer.positionLocked) doc.activeLayer.positionLocked = false;

    if(doc.activeLayer.transparentPixelsLocked) doc.activeLayer.transparentPixelsLocked = false;

    doc.activeLayer.visible =  false;

}

JJMack
Community Expert
Community Expert
September 27, 2010

ishook187 wrote:

I've been looking at the similar questions, but no script has worked perfectly yet. I have a series of phoshop actions that I run on a regular basis. I have two version of each, one for if there's a locked background layer, and one for when its already been unlocked. Otherwise my actions error.

A Photoshop background layer can not be completely unlocked because a Photoshop background layer does not support transparency that will alway be locked at 100% opacity and fill and can not be changed, no layer can be moved below it and it can not be moved up in the layer stack..  You can convert a background layer to a normal layer which will unlock the layer but it is no longer a background layer.  It is very easy in a script to target the bottom layer and make it a  normal layer just by setting the bottom layer not to be a background.

JJMack
September 27, 2010

I understand the limitations of the Background layer, and don't have a need to keep any properties of the Background layer. The limitations of the background layer have led me to the forums to find a script that can eliminate the limitations. In the end, I'd like to end up with an unlocked Layer 0, without first having to select the Background layer. It should also not error if there isn't a background layer to begin with (like a previously worked on photoshop file)