问题
Unlike this question, is there a way to return an array of currently selected layers, without having to loop over all the layers and therefore removing/readjusting that selection?
function get_selected_layers()
{
var layers = app.activeDocument.activeLayer;
// only works with *last* selected layer
// not *all* selected layers
}
var thelayers = get_selected_layers;
alert(thelayers);
回答1:
Sort of but not really. There's no native way of getting an array of selected artLayer
objects. But there's a Action Manager way of getting descriptors of selected layers — and from descriptors you can get everything you want. Here's a snippet that returns an array of selected layers names, indexes and ids — using different getters on layer descriptor (desc
) it's possible to get everything else. As a bonus a function to select artLayers
by IDs if you want to get artLayer
DOM objects. And yes, this works on groups and artboards.
var layers = getSelectedLayersInfo();
// if we _really_ want to get artLayers we can select them one by one with IDs
for (var i = 0; i < layers.length; i++) {
selectByID(layers[i].id);
alert(activeDocument.activeLayer.name);
}
// and reselecting everything back
for (var i = 0; i < layers.length; i++) {
selectByID(layers[i].id, true);
}
function getSelectedLayersInfo()
{
var lyrs = [];
var lyr;
var ref = new ActionReference();
var desc;
var tempIndex = 0;
var ref2;
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var targetLayers = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));
for (var i = 0; i < targetLayers.count; i++)
{
ref2 = new ActionReference();
// if there's a background layer in the document, AM indices start with 1, without it from 0. ¯\_(ツ)_/¯
try
{
activeDocument.backgroundLayer;
ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex());
desc = executeActionGet(ref2);
tempIndex = desc.getInteger(stringIDToTypeID("itemIndex")) - 1;
}
catch (o)
{
ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex() + 1);
desc = executeActionGet(ref2);
tempIndex = desc.getInteger(stringIDToTypeID("itemIndex"));
}
lyr = {};
lyr.index = tempIndex;
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyrs.push(lyr);
}
return lyrs;
}
function selectByID(id, add) {
if (add == undefined) add = false;
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
if (add) desc1.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
} // end of selectByID()
来源:https://stackoverflow.com/questions/63448143/get-selected-layers