Hi, guys!
I need to find all of the layers in the document that start with a "$".
I do this by comparing the name of each layer using regexps (see code).
But it turns out very slowly. Maybe there is a quick way to do this?
Thank U!
(sorry google translate )
Code: Select all#target photoshop
docRef = app.activeDocument;
var arrTemp= new Array();
for(var i=0; i<docRef.artLayers.length; i++)if(docRef.artLayers.name.match(/\$.*/) != null) arrTemp.push(docRef.artLayers);
Fastest way to get the layers by keystring (regexp, etc)?
Fastest way to get the layers by keystring (regexp, etc)?
Does this help?
It collects the names and indices of the layers instead of the Layers, the Layers can later be addressed though their indices (if the number and position of Layers get changed the indices may change, so that might necessitate using the layerID instead).
Code: Select all// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
main ();
};
////////////////////////////////////
function main (layerName) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// anumber is intended to keep track of layerset depth;
var aNumber = 0;
var theArray = new Array;
var theGroups = new Array;
////// work through layers //////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
////////////////////////////////////
// if neither group start or end;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" /*&& isBackground != true*/) {
// if the name matches collect index;
if(theName.match(/\$.*/) != null) {theArray.push([m, theName])};
};
////////////////////////////////////
}
catch (e) {};
};
// the results;
alert ("the layer/s have the index/indices and name/s:"+"\n"+theArray.join("\n"));
};
// by mike hale, via paul riggott;
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
It collects the names and indices of the layers instead of the Layers, the Layers can later be addressed though their indices (if the number and position of Layers get changed the indices may change, so that might necessitate using the layerID instead).
Code: Select all// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
main ();
};
////////////////////////////////////
function main (layerName) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// anumber is intended to keep track of layerset depth;
var aNumber = 0;
var theArray = new Array;
var theGroups = new Array;
////// work through layers //////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
////////////////////////////////////
// if neither group start or end;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" /*&& isBackground != true*/) {
// if the name matches collect index;
if(theName.match(/\$.*/) != null) {theArray.push([m, theName])};
};
////////////////////////////////////
}
catch (e) {};
};
// the results;
alert ("the layer/s have the index/indices and name/s:"+"\n"+theArray.join("\n"));
};
// by mike hale, via paul riggott;
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};