skip to Main Content

i’m stuck with a script that detects which layer is visible in a (sub)layerset (aka Group) with the name “Color”.

The script below checks for all visible layers and selects them. I can’t get it working to do the same thing ONLY in the mentioned layerset.

Any help would be highly appreciated!

#target photoshop
app.bringToFront();

main();
function main(){
if(!documents.length) return;
var Vis = getVisLayers();
deselectLayers();
for(var a in Vis){
    selectLayerById(Number(Vis[a]),true);
    }
}
function getVisLayers(){ 
   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;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        var vis = desc.getBoolean(charIDToTypeID( "Vsbl" ));
        if(!isLayerSet && vis) Names.push(Id);
   }; 
return Names;
};
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), ID);
   var desc = new ActionDescriptor();
   desc.putReference(charIDToTypeID('null'), ref);
   if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
   }
   desc.putBoolean(charIDToTypeID('MkVs'), false);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}
function deselectLayers() { 
    var desc01 = new ActionDescriptor(); 
        var ref01 = new ActionReference(); 
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
    desc01.putReference( charIDToTypeID('null'), ref01 ); 
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO ); 
};

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Ghoul Fool i finally managed to get this working. I changed the code a bit as i only need to check which layer is visible and store this name as a variable. Would be great if someone more skilled could correct my code for others who will use it or parts of it.

    Thanks again

    // Checking which layer is visible in layeset (group) "Colors" in "Mockup.psd" aka mockupDoc and store it's name for later use in the "save" part.
    var front = mockupDoc.layerSets.getByName("Front");
    var colors = front.layerSets.getByName("Colors");
    for (var m = colors.layers.length - 1; m >= 0; m--)
    {
      var theLayer = colors.layers[m];
      if (theLayer.typename == "ArtLayer")
     {
        if (theLayer.visible == true)
        {
          // Sets a variable for the name of the visible Layer in the "Colors" Group, so i can use it later as a part of the Filename when saving.
          var activeColor = theLayer.name;
        }
      }
    };
    

  2. Trouble with groups is that you have to jump at the deep end. Sadly the way Photoshop keeps track of it’s layers isn’t intuitive. The best thing to do is have a recursive function and look over ALL the layers – from that you’ll be able to determine if it’s a group or not. And then work out if it’s visible.

    var allLayers = new Array();
    var theLayers = collectAllLayers(app.activeDocument, 0);
    
    // function collect all layers
    function collectAllLayers (theParent, level)
    {
      for (var m = theParent.layers.length - 1; m >= 0; m--)
      {
        var theLayer = theParent.layers[m];
    
        if (theLayer.typename == "ArtLayer")
        {
          // find the art layers
          if (theLayer.visible == true)
          {
          //do stuff here
          }
        }
        else
        {
          allLayers.push(level + theLayer.name);
          collectAllLayers(theLayer, level + 1)
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search