javascript 如何选择活动层?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15312279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:19:42  来源:igfitidea点击:

How to select active layer?

javascriptphotoshopphotoshop-script

提问by Pablo

It appears that in order to resize the layer it has to be selected(from UI perspective) and active from API perspective. Otherwise I get error on any function call that this function is not supported.

似乎为了调整图层的大小,它必须被选中(从 UI 角度)并从 API 角度激活。否则,我在任何不支持此函数的函数调用中都会出错。

So before resizing I do

所以在调整大小之前我做

var a = doc.artLayers.getByName("iPad");
app.activeDocument.activeLayer = a;

This doesn't visually change selected layer hence calling resize function fails after that. The only way to get it work, manually click on layer(any layer), then it works. What is the proper way to resize layer without user interaction?

这不会在视觉上更改所选图层,因此在此之后调用调整大小函数失败。让它工作的唯一方法,手动点击图层(任何图层),然后它就可以工作了。在没有用户交互的情况下调整图层大小的正确方法是什么?

回答by Ghoul Fool

You just need to amend your code:

你只需要修改你的代码:

var doc = app.activeDocument;
doc.activeLayer = doc.artLayers.getByName("iPad");

This will set the active layer to the one named "ipad". This is a standard way of selecting a layer (by name) to then further process the image, in your case resizing it. Obviously I don't know what else in in the PSD in terms of layers to pick or ignore. Another way would be to iterate through all layers and process them all.

这会将活动层设置为名为“ipad”的层。这是选择图层(按名称)然后进一步处理图像的标准方法,在您的情况下调整它的大小。显然,我不知道 PSD 中还有什么要选择或忽略的层。另一种方法是遍历所有层并处理它们。

Here are two useful functions: One will select the layer mask is there is one, the other will deselect the layer mask and go back to the bitmap layer

这里有两个有用的功能:一个是选择图层蒙版,另一个是取消选择图层蒙版并返回位图图层

// FUNCTION DESELECT LAYER MASK AND SELECT IMAGE LAYER
// --------------------------------------------------------
function deselectLayerMaskAndSelectImageLayer()
{
  // =======================================================
  var id248 = charIDToTypeID( "slct" );
  var desc48 = new ActionDescriptor();
  var id249 = charIDToTypeID( "null" );
  var ref36 = new ActionReference();
  var id250 = charIDToTypeID( "Chnl" );
  var id251 = charIDToTypeID( "Chnl" );
  var id252 = charIDToTypeID( "RGB " );
  ref36.putEnumerated( id250, id251, id252 );
  desc48.putReference( id249, ref36 );
  var id253 = charIDToTypeID( "MkVs" );
  desc48.putBoolean( id253, false );
  executeAction( id248, desc48, DialogModes.NO );
}


// FUNCTION SELECT MASK
// --------------------------------------------------------
function selectMask(LayerName)
{
  try
  {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    ref.putName( charIDToTypeID('Lyr '), LayerName );
    desc.putReference( charIDToTypeID('null'), ref );
    desc.putBoolean( charIDToTypeID('MkVs'), true );
    executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );

    // =======================================================
    var id1083 = charIDToTypeID( "setd" );
    var desc238 = new ActionDescriptor();
    var id1084 = charIDToTypeID( "null" );
    var ref161 = new ActionReference();
    var id1085 = charIDToTypeID( "Chnl" );
    var id1086 = charIDToTypeID( "fsel" );
    ref161.putProperty( id1085, id1086 );
    desc238.putReference( id1084, ref161 );
    var id1087 = charIDToTypeID( "T   " );
    var ref162 = new ActionReference();
    var id1088 = charIDToTypeID( "Chnl" );
    var id1089 = charIDToTypeID( "Ordn" );
    var id1090 = charIDToTypeID( "Trgt" );
    ref162.putEnumerated( id1088, id1089, id1090 );
    desc238.putReference( id1087, ref162 );
    executeAction( id1083, desc238, DialogModes.NO );
  }
  catch(e)
  {
  //alert(e)
  //alert( "This layer has NO layer mask!" );
  activeDocument.selection.deselect();
  }
} //end function

回答by addme

You can find it in the Data Browser view from extendscript:

您可以在扩展脚本的数据浏览器视图中找到它:

var doc = app.activeDocument;

// set active layer
doc.activeLayer = doc.layers.getByName("Layer Name Here");