admin管理员组文章数量:1279210
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.
I know the document holds something like "activeLayers" but I get lost how to deal with levels and sublevel layers.
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
Thanks for any tips on this.
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.
I know the document holds something like "activeLayers" but I get lost how to deal with levels and sublevel layers.
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
Thanks for any tips on this.
Share Improve this question asked Dec 2, 2014 at 17:26 Daniel GodinhoDaniel Godinho 311 silver badge2 bronze badges3 Answers
Reset to default 6Bravo'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());
Simply wrapping the code in a try/catch block didnt work, so I also changed:
executeAction( idGrp, descGrp, DialogModes.ALL );
to
executeAction( idGrp, descGrp, DialogModes.NO );
and that made the runtime error go away.
This will get you the selected layers
function getSelectedLayers(){
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.ALL );
var resultLayers=new Array();
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 );
return resultLayers;
}
var layers = getSelectedLayers();
Source: https://github./kynd/photoshopScripts/blob/master/Rename%20Selected%20Layers/Rename%20Selected%20Layers.jsx
You'll need to check out the 'ArtLayers' and 'LayerSets' objects. The following snippet will get all the layers in the first group within a document:
var lyrs = app.activeDocument.layerSets[0].artLayers;
But since each of your groups can contain other groups, you may have to recursively loop through all of them to get all your layers depending on your use case.
I highly remend checking out the xtools library. It has a nice function 'Stdlib.getLayersList' that will allow you to get the all the layers recursively from nested groups (plus a whole lot of other great stuff). Get the library here.
本文标签: Getting Selected Layer or group layers array using javascript (Photoshop CS4)Stack Overflow
版权声明:本文标题:Getting Selected Layer or group layers array using javascript (Photoshop CS4) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741253458a2366222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论