获取选定图层

Get selected layers

不像这个question,有没有办法return当前选中层的数组,没有 必须遍历所有图层,因此 removing/readjusting 那个选择?

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

有点,但不是真的。没有获取 selected artLayer 对象数组的本地方法。但是有一种获取 selected 层的描述符的 Action Manager 方法——从描述符中你可以得到你想要的一切。这是一个片段,其中 returns 一个包含 select 层名称、索引和 ID 的数组——在层描述符 (desc) 上使用不同的 getter 可以获得其他所有内容。如果你想获得 artLayer DOM 个对象,作为 select artLayers 的额外功能。是的,这适用于群组和画板。

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