如何基于Javascript在Photoshop中选择子图层?

时间:2018-12-03 05:25:49

标签: javascript photoshop

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["组5"]; //this defines the layer that you want to get the selection from  

var myLayer = app.activeDocument.layers["组5"];

//alert(myLayer.layers);


docRef.selection = null;//这句是让你没有选中任何图层

for (var i=0;i<myLayer.layers.length;i++){


          if (myLayer.layers[i].name=="图层"){  //   alert(myLayer.layers[i].name=="图层");
        //  alert(myLayer.layers[i].name);
          myLayer.layers[i].selected=true;
     }

}

我有这样的代码,当在photoshop cs中,某些子层的名称等于“图层”时,则应选择此子层,但不起作用,谁知道如何选择它们?

1 个答案:

答案 0 :(得分:0)

据我所知,使用DOM无法做到这一点,但是Action Manager代码将完成此任务:

var myName = "Layer 5";

deselectLayers(); // deselecting all layers first

traverseAllLayers(myName);

function selectByID(id)
{
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID('Lyr '), id);
    desc.putReference(charIDToTypeID('null'), ref);
    desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
    executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
};

function deselectLayers()
{
    var desc60 = new ActionDescriptor();
    var ref30 = new ActionReference();
    ref30.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
    desc60.putReference(charIDToTypeID('null'), ref30);
    executeAction(stringIDToTypeID('selectNoLayers'), desc60, DialogModes.NO);
};

function traverseAllLayers(n)
{
    var ref0 = new ActionReference();
    ref0.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('numberOfLayers'));
    ref0.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc0 = executeActionGet(ref0);
    var i = desc0.getInteger(stringIDToTypeID('numberOfLayers'));
    for (i; i > 0; i--)
    {
        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 == n) selectByID(Id) // selecting by ID, adding to selection
    }
}