使用javascript获取选定的图层或组图层阵列(Photoshop CS4) [英] Getting Selected Layer or group layers array using javascript (Photoshop CS4)
问题描述
K基本上,我需要编写一个photoshop脚本,该脚本遍历所选图层并检索有关它们的一些信息.
K basically I need to write a photoshop script that iterates through the selected layers and retrieve some information about them.
如果我可以得到一个选定图层的数组,我会很好的,我知道如何做剩下的事情,但是要获得该数组本身是一件令人头疼的事.
if I can get an array of the selected layers I will be fine, I know how to do the rest, but getting that array is been a headache on its own.
我知道文档中包含"activeLayers"之类的东西,但是我迷路了如何处理级别和子级别层.
I know the document holds something like "activeLayers" but I get lost how to deal with levels and sublevel layers.
所有图层都在一个组中,所以如果方便的话,如果我可以将一组中的每个图层都变得很棒:D
All the layers are in a group, so if its easier, if I can get every layer in a group that would be great too :D
谢谢你的提示.
推荐答案
Bravo的代码可以正常工作,只是在未选择任何层时它会因错误而失败,因此我对其进行了调整以消除错误并仅返回一个空数组:
Bravo's code works except it fails with an error when no layers are selected, so I tweaked it to remove the error and simply return an empty array:
function getSelectedLayers(){
var resultLayers=new Array();
try{
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.NO );
for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
var id8 = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var id9 = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var id10 = charIDToTypeID( "HstS" );
var id11 = charIDToTypeID( "Ordn" );
var id12 = charIDToTypeID( "Prvs" );
ref2.putEnumerated( id10, id11, id12 );
desc5.putReference( id9, ref2 );
executeAction( id8, desc5, DialogModes.NO );
} catch (err) { }
return resultLayers;
}
$.writeln(getSelectedLayers());`
仅将代码包装在try/catch块中是行不通的,所以我也进行了更改:
Simply wrapping the code in a try/catch block didnt work, so I also changed:
executeAction( idGrp, descGrp, DialogModes.ALL );
到
executeAction( idGrp, descGrp, DialogModes.NO );
从而消除了运行时错误.
and that made the runtime error go away.
这篇关于使用javascript获取选定的图层或组图层阵列(Photoshop CS4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!