Skip to main content
Participant
September 27, 2014
Answered

Delete all groups and layers that are switched off, even if they are locked

  • September 27, 2014
  • 1 reply
  • 1196 views

Hi,

I need help with a script that will do the following:

Delete all groups (including layers inside) and layers if they are are not visible - with the following requirements

a. Even if the group or layer is locked

b. Even if layers inside the group are locked

c. Even if layers inside the group are visible, though the parent group is switched off

So...the logic required would be

Is this group or layer switched off? yes, then delete it all regardless of anything else, ELSE move on to the next group/layer

Basically, i have lots of groups and layers here and i want to parse the layers to leave just the layers which are visible in the final document. Merge visible or flatten will not work as i want to retain the visible groups, and visible layers. Delete hidden layers does not work. And file/scrips/delete empty layers does not work because the layers are not empty. They're just not required.

Working with CS6.

Thanks

This topic has been closed for replies.
Correct answer c.pfaffenbichler

The other was round, but this should unlock all layers, after that delete hidden layers should work.

// unlock all layers;

// based on code to link layer masks by paul riggott;

// 2014, use it at your own risk;

#target "photoshop-70.032"

app.bringToFront();

if(app.documents.length != 0) {

  unlockLayers();

  };

////// unlock layers //////

function unlockLayers(){

  var ref = new ActionReference();

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

  var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

  var Names=[];

  try{

  activeDocument.backgroundLayer;

  var i = 0; }catch(e){ var i = 1; };

  for(i;i<count;i++){

  if(i == 0) continue;

  ref = new ActionReference();

  ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

  var desc = executeActionGet(ref);

  var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

  var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));

  if(layerName.match(/^<\/Layer group/) ) continue;

  unlockLayer (i);

  }

  };

////// unlock layer by index //////

function unlockLayer (idx){

selectLayerByIndex(idx, false);

try {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), idx );

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

var desc4 = new ActionDescriptor();

var desc5 = new ActionDescriptor();

var idprotectNone = stringIDToTypeID( "protectNone" );

desc5.putBoolean( idprotectNone, true );

var idlayerLocking = stringIDToTypeID( "layerLocking" );

desc4.putObject( idlayerLocking, idlayerLocking, desc5 );

desc.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Lyr " ), desc4 );

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

} catch (e) {alert (e)};

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

    var desc = new ActionDescriptor();

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

       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

      desc.putBoolean( charIDToTypeID( "MkVs" ), false );

   try{

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

}catch(e){

alert(e.message);

}

};

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
September 28, 2014

The other was round, but this should unlock all layers, after that delete hidden layers should work.

// unlock all layers;

// based on code to link layer masks by paul riggott;

// 2014, use it at your own risk;

#target "photoshop-70.032"

app.bringToFront();

if(app.documents.length != 0) {

  unlockLayers();

  };

////// unlock layers //////

function unlockLayers(){

  var ref = new ActionReference();

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

  var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

  var Names=[];

  try{

  activeDocument.backgroundLayer;

  var i = 0; }catch(e){ var i = 1; };

  for(i;i<count;i++){

  if(i == 0) continue;

  ref = new ActionReference();

  ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

  var desc = executeActionGet(ref);

  var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

  var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));

  if(layerName.match(/^<\/Layer group/) ) continue;

  unlockLayer (i);

  }

  };

////// unlock layer by index //////

function unlockLayer (idx){

selectLayerByIndex(idx, false);

try {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), idx );

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

var desc4 = new ActionDescriptor();

var desc5 = new ActionDescriptor();

var idprotectNone = stringIDToTypeID( "protectNone" );

desc5.putBoolean( idprotectNone, true );

var idlayerLocking = stringIDToTypeID( "layerLocking" );

desc4.putObject( idlayerLocking, idlayerLocking, desc5 );

desc.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Lyr " ), desc4 );

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

} catch (e) {alert (e)};

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

    var desc = new ActionDescriptor();

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

       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

      desc.putBoolean( charIDToTypeID( "MkVs" ), false );

   try{

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

}catch(e){

alert(e.message);

}

};

danzooAuthor
Participant
September 28, 2014

Looks promising but it doesn't seem to delete the hidden groups.

I have made a photoshop file with all the layer/groups with instructions linked below.

Basically, i just want the green layers left on this psd file after the script runs.

http://ge.tt/9bgWtoz1/v/0?c

Can this be done with photoshop scripting? I just want to purge the documents to get them to a manageable size and make them more workable. Some of them have hundreds of hidden layers / groups which is making the psd size way too big to collaborate with.

Thanks for any help. Dan

c.pfaffenbichler
Community Expert
Community Expert
September 29, 2014

Looks promising but it doesn't seem to delete the hidden groups.

As I stated the Script I posted unlocks the Layers, adding the "Delete Hidden Layers" you’d have to do yourself.