我正在尝试创建一个脚本,用于处理带有反转的层。在Photoshop中,这是使用ctrl +单击图层来完成的。我的文档中的所有层都被分成几个组。正因为如此,出现了一个错误(photoshop正在试图反转组)。如何在循环中跳过组,或者如何修复它?
var doc = app.activeDocument;
var layers =activeDocument.layers;
for (var layerIndex = 0; layerIndex<doc.layers.length; layerIndex++)
{
var layer = layers [layerIndex];
doc.activeLayer = layer;
var idChnl = charIDToTypeID( "Chnl" );
var actionSelect = new ActionReference();
actionSelect.putProperty( idChnl, charIDToTypeID( "fsel" ) );
var actionTransparent = new ActionReference();
actionTransparent.putEnumerated( idChnl, idChnl, charIDToTypeID( "Trsp" ) );
var actionDesc = new ActionDescriptor();
actionDesc.putReference( charIDToTypeID( "null" ), actionSelect );
actionDesc.putReference( charIDToTypeID( "T " ), actionTransparent );
executeAction( charIDToTypeID( "setd" ), actionDesc, DialogModes.NO );
var invert = doc.selection.invert(actionSelect);
var clear = doc.selection.clear(invert);
var des = doc.selection.deselect(clear);
//При попытке выделить группу возникает ошибка, нужно сделать пропуск группы
} 发布于 2022-09-02 15:50:09
在Photoshop中,为了编写脚本,普通层是"ArtLayer“类型,组是"LayerSet”类型。组(LayerSet)可以是包含多层(ArtLayer)甚至多个组的数组。
如何在循环中跳过组?
因此,在for循环中,您希望通过检查当前层的typename来了解当前层是实际层还是组:
...
var layer = layers [layerIndex];
if (layer.typename == "LayerSet") { //if current layer is a group
continue; //skip to next loop element
}
doc.activeLayer = layer;
...但是,如果文档中的所有层都被划分为组,则当前的for循环将不会遍历组内的层,因为您的层变量仅包含顶层层(activeDocument.layers)。
由于这些顶层层可以是一个层,也可以是一个组,而且上面的代码跳过任何组,所以您的循环将只遍历任何顶层层(如果有的话),忽略组内的任何层(因为组被跳过)。
换句话说,如果文档的结构使得所有顶层层都是组,则循环将跳过所有这些层,并且不会处理任何层。
我怎么才能修好它?
您可能希望通过检查该层是否是一个组来修复您的循环,如果是的话,也可以在组的层中循环。解决这个问题的方法有很多种,这取决于您希望使用递归函数获得多大的复杂性,但为了简化,只需创建一个嵌套的for循环:
...
var layer = layers [layerIndex];
if (layer.typename == "LayerSet") { //if current layer is a group
//if group, loop through its layers
var groupLayers = layer.layers;
for (var i = 0; i < groupLayers.length; i++) {
var layer = groupLayers[i];
doc.activeLayer = layer;
// do your processing here
} //end group layers loop
} //end if LayerSet
...由于您的处理代码可能也适用于其他情况,例如,如果您在组之外有顶级层,那么最好将其提取到它自己的函数中,而不是将它保存在for循环中,然后简单地调用循环中的函数或其他可能应用的函数。下面是整个代码的样子:
var doc = app.activeDocument;
var layers = doc.layers;
for (var layerIndex = 0; layerIndex < layers.length; layerIndex++)
{
var layer = layers[layerIndex];
if (layer.typename == "LayerSet") { //if layer is a group
//if group, loop through its layers
var groupLayers = layer.layers;
/** Note that a group could also have nested groups,
which is not accounted for in this scenario **/
for (var i = 0; i < groupLayers.length; i++) {
var layer = groupLayers[i];
doc.activeLayer = layer;
// do your processing here
processLayer();
} //end group layers loop
} //end if LayerSet
} //end doc layers loop
//Function for inverting active layer (or whatever your function does)
function processLayer(){
var idChnl = charIDToTypeID( "Chnl" );
var actionSelect = new ActionReference();
actionSelect.putProperty( idChnl, charIDToTypeID( "fsel" ) );
var actionTransparent = new ActionReference();
actionTransparent.putEnumerated( idChnl, idChnl, charIDToTypeID( "Trsp" ) );
var actionDesc = new ActionDescriptor();
actionDesc.putReference( charIDToTypeID( "null" ), actionSelect );
actionDesc.putReference( charIDToTypeID( "T " ), actionTransparent );
executeAction( charIDToTypeID( "setd" ), actionDesc, DialogModes.NO );
var invert = doc.selection.invert(actionSelect);
var clear = doc.selection.clear(invert);
var des = doc.selection.deselect(clear);
return;
} //end processLayer()FYI:在开发Photoshop脚本时有一个Photoshop Javascript参考的副本是个好主意。您可以使用引用pdf查看ArtLayer层和LayerSets (组)层可用的所有属性和方法,如名称、不透明度、是否可见等。
https://stackoverflow.com/questions/73513883
复制相似问题