select layer 1 or layer 2

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

flieckster

select layer 1 or layer 2

Post by flieckster »

i'm super new to scripting and i'm wondering how i set up a script to select 'layer 1' or 'layer 2'. i get files back from vendors and i can't action them because sometimes the layer i need is named either layer 1 or layer 2.

after that i can run a normal PS action.

thanks!
pfaffenbichler

select layer 1 or layer 2

Post by pfaffenbichler »

What are the characteristics the affected Layers will definitely always have (name, number of Layers, which kind of Layers, positioned top-level or in Groups, total number, will there be other Layers that share those characteristics, …)?

Edit:
Does this help?
Code: Select all// get index of layer/s of specified name and select layers;
// 2014, use it at your own risk;
#target "photoshop-70.032"
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;
////// 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'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
////////////////////////////////////
// if neither group start or end;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" /*&& isBackground != true*/) {
// if the name matches collect index;
if(theName == "layer 1" || theName == "layer 2") {theArray.push([m, theName])};
};
////////////////////////////////////
}
catch (e) {};
};
// select the results;
if (theArray.length > 0) {
selectLayerByIndex(theArray[0][0],false);
for (var x = 1; x < theArray.length; x++) {
selectLayerByIndex(theArray[x][0],true)
}
};
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
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);
}
};