Photoshop脚本 - 获取组/层中可见图层的名称

时间:2017-03-25 13:46:53

标签: javascript javascript-objects photoshop-script

我坚持使用一个脚本来检测哪个图层在(子)layerset(aka Group)中可见,名称为“Color”。

下面的脚本会检查所有可见图层并选择它们。我不能让它只在提到的layerset中做同样的事情。

任何帮助都将受到高度赞赏!

#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 个答案:

答案 0 :(得分:0)

群组的问题在于你必须跳到最深处。可悲的是,Photoshop跟踪它的图层的方式并不直观。最好的办法是使用递归函数并查看所有图层 - 从中​​您可以确定它是否是一个组。然后解决它是否可见。

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)
    }
  }
}

答案 1 :(得分:0)

感谢@Ghoul Fool,我终于成功了。我稍微更改了代码,因为我只需要检查哪个图层可见并将此名称存储为变量。如果有技能的人可以为使用它或部分内容的其他人纠正我的代码,那就太棒了。

再次感谢

// 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;
    }
  }
};