Skip to main content
Known Participant
November 6, 2023
Answered

Is it possible to do photoshop scripts to find layer name?

  • November 6, 2023
  • 1 reply
  • 898 views
Can anyone help? 
Is it possible to do photoshop scripts to find layer name?
This is because it is difficult to build Action

 

This topic has been closed for replies.
Correct answer Stephen Marsh

A great script from Paul Riggott:

 

/*///////////////////////////////////////////////////////////////////////////////////
Written by Paul Riggott
Functions by Mike Hale
French translation by Google
////////////////////////////////////////////////////////////////////////////////////*/
#target photoshop
function main(){
var win = new Window("dialog");
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.THEME_COLOR, "appDialogBackground");
g.backgroundColor = myBrush;
win.title = win.add('statictext',undefined, 'LayerName Find');
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.p1  = win.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.grp0 =  win.p1.add("group");
win.p1.grp0.orientation='row';
win.p1.grp0.alignChildren='right';
win.p1.grp0.spacing=10;
win.p1.grp0.rb1 = win.p1.grp0.add('radiobutton',undefined,'Case Sensitive');
win.p1.grp0.rb2 = win.p1.grp0.add('radiobutton',undefined,'Not Case Sensitive');
win.p1.grp0.cb1 = win.p1.grp0.add('checkbox',undefined,'Select all occurences');
win.p1.grp0.rb1.value=true;
win.p1.grp0.cb1.value=true;
win.p1.grp1 =  win.p1.add("group");
win.p1.grp1.orientation='row';
win.p1.grp1.alignment='fill';
win.p1.grp1.spacing=10;
win.p1.grp1.st1 = win.p1.grp1.add('statictext',undefined,'Layer Name:');
win.p1.grp1.st1.helpTip= 'All layers with the same name will be selected';
win.p1.grp1.et1 = win.p1.grp1.add('edittext');
win.p1.grp1.et1.preferredSize=[300,20];
win.p1.grp1.et1.active=true;
win.p1.grp2 =  win.p1.add("group");
win.p1.grp2.orientation='row';
win.p1.grp2.spacing=10;
win.p1.grp2.bu1 = win.p1.grp2.add('button',undefined,'Select');
win.p1.grp2.bu1.preferredSize=[190,25];
win.p1.grp2.bu2 = win.p1.grp2.add('button',undefined,'Cancel');
win.p1.grp2.bu2.preferredSize=[190,25];
win.p1.grp2.bu1.onClick = function(){
    if(win.p1.grp1.et1.text == ''){
        alert('No Layer Name Has Been Entered!');
        win.p1.grp1.et1.active=true;
        return;
        }
        win.close(1);
        process();
   }
win.p1.grp2.bu2.onClick=function(){
        win.close(2);
}
win.center();
win.show();
function process(){

if(win.p1.grp0.rb1.value){
var re = new RegExp (win.p1.grp1.et1.text.toString());
}else{
    var re = new RegExp ("^"+win.p1.grp1.et1.text.toString(),"i");
    }
var matches = collectNamesAM(re);
if(!matches.length){
    alert('Layer \"' + win.p1.grp1.et1.text.toString()+ '\" was not found!');
    return;
    }
if(win.p1.grp0.cb1.value){
if(activeDocument.layerSets.length > 1){
for(var a in matches){
   makeActiveByIndex(matches[a], false );
    }
}
for( var l = 0; l < matches.length; l++ ){
    if(l == 0) {
        makeActiveByIndex(matches[l], false );
        }else{
            makeActiveByIndex(matches[l], false,true );
            }  
 }
}else{
    if(matches.length == 1) {
        makeActiveByIndex(matches[0], false );
        }else{
 var win2 = new Window("dialog");
g = win2.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win2.title = win2.add('statictext',undefined,'LayerName Find');
var g = win2.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win2.p1  = win2.add("panel", undefined, undefined, {borderStyle:"black"});
win2.p1.grp0 =  win2.p1.add("group");
win2.p1.grp0.orientation='row';
win2.p1.grp0.alignChildren='fill';
win2.p1.grp0.spacing=10;
win2.p1.grp0.st1 = win2.p1.grp0.add('statictext',undefined, 'Please select layer');
win2.p1.grp0.st1.graphics.font=ScriptUI.newFont("Georgia","BOLDITALIC",16);
win2.p1.grp0.dd1 = win2.p1.grp0.add('dropdownlist');
for(var a = 1; a< matches.length+1;a++){
    win2.p1.grp0.dd1.add('item', a);
}
win2.p1.grp0.dd1.selection=0;
win2.p1.grp2 =  win2.p1.add("group");
win2.p1.grp2.orientation='row';
win2.p1.grp2.spacing=10;
win2.p1.grp2.bu1 = win2.p1.grp2.add('button',undefined,'Select');
win2.p1.grp2.bu1.preferredSize=[100,25];
win2.p1.grp2.bu2 = win2.p1.grp2.add('button',undefined,'Cancel');
win2.p1.grp2.bu2.preferredSize=[100,25];
win2.p1.grp2.bu2.onClick=function(){
        win2.close(2);
}
win2.p1.grp2.bu1.onClick=function(){
    win2.close(1);
makeActiveByIndex(matches[Number(win2.p1.grp0.dd1.selection.index)], false );
}
win2.center();
win2.show();
}
    }
    }

function collectNamesAM(re){
     var allLayers = new Array();
   var startLoop = Number( !hasBackground() );
   var endLoop = getNumberOfLayer(); 
   for( var l = startLoop;l < endLoop; l++){
        while( !isValidActiveLayer( l ) ) {
            l++;
        }
        if( getLayerNameByIndex( l ).match(re) != null){
            if(getLayerNameByIndex( l ).length == win.p1.grp1.et1.text.toString().length){
            allLayers.push( l );
            }
        }
    }
     return allLayers;
};
}
function getActiveLayerIndex() {
     var ref = new ActionReference();
     ref.putProperty( 1349677170 , 1232366921 );
     ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
     var res = executeActionGet(ref).getInteger( 1232366921 ) - Number( hasBackground() );
     res == 4 ? res++:res;
     return res;   
}
function isValidActiveLayer( idx ) {
     var propName = stringIDToTypeID( 'layerSection' );
     var ref = new ActionReference(); 
     ref.putProperty( 1349677170 , propName);
     ref.putIndex( 1283027488, idx );
     var desc =  executeActionGet( ref );
     var type = desc.getEnumerationValue( propName );
     var res = typeIDToStringID( type ); 
     return res == 'layerSectionEnd' ? false:true;
};
function hasBackground(){
    var res = undefined;
    try{
        var ref = new ActionReference();
        ref.putProperty( 1349677170 , 1315774496); 
        ref.putIndex( 1283027488, 0 );
        executeActionGet(ref).getString(1315774496 );
        res = true;
    }catch(e){ res = false}
    return res;
};
function makeActiveByIndex( idx, forceVisible,add ){
    add == undefined ? add = false : add = true
     try{
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putIndex(charIDToTypeID( "Lyr " ), idx)
          desc.putReference( charIDToTypeID( "null" ), ref );
          if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
          desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible );
          executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
     }catch(e){ return -1;}
};
function getNumberOfLayer(){ 
var ref = new ActionReference(); 
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref); 
var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL")); 
return numberOfLayer; 
};
function getLayerNameByIndex( idx ) { 
    var ref = new ActionReference(); 
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " )); 
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));; 
};

main()

 

1 reply

Stephen Marsh
Community Expert
Community Expert
November 6, 2023

@Thihasoe 

 

Yes, it would have been nice if Adobe had provided access to the layer filtering via actions or scripting.

 

It is indeed possible to use scripting to select a layer by name:

 

#target photoshop
var layerName = prompt('Enter a Layer Name to Select', '');
app.activeDocument.activeLayer = app.activeDocument.layers.getByName(layerName);

 

The caveat is that the first layer that matches will be selected if there are multiples with the same name.

 

You may also find this topic of interest:

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 6, 2023

A great script from Paul Riggott:

 

/*///////////////////////////////////////////////////////////////////////////////////
Written by Paul Riggott
Functions by Mike Hale
French translation by Google
////////////////////////////////////////////////////////////////////////////////////*/
#target photoshop
function main(){
var win = new Window("dialog");
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.THEME_COLOR, "appDialogBackground");
g.backgroundColor = myBrush;
win.title = win.add('statictext',undefined, 'LayerName Find');
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.p1  = win.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.grp0 =  win.p1.add("group");
win.p1.grp0.orientation='row';
win.p1.grp0.alignChildren='right';
win.p1.grp0.spacing=10;
win.p1.grp0.rb1 = win.p1.grp0.add('radiobutton',undefined,'Case Sensitive');
win.p1.grp0.rb2 = win.p1.grp0.add('radiobutton',undefined,'Not Case Sensitive');
win.p1.grp0.cb1 = win.p1.grp0.add('checkbox',undefined,'Select all occurences');
win.p1.grp0.rb1.value=true;
win.p1.grp0.cb1.value=true;
win.p1.grp1 =  win.p1.add("group");
win.p1.grp1.orientation='row';
win.p1.grp1.alignment='fill';
win.p1.grp1.spacing=10;
win.p1.grp1.st1 = win.p1.grp1.add('statictext',undefined,'Layer Name:');
win.p1.grp1.st1.helpTip= 'All layers with the same name will be selected';
win.p1.grp1.et1 = win.p1.grp1.add('edittext');
win.p1.grp1.et1.preferredSize=[300,20];
win.p1.grp1.et1.active=true;
win.p1.grp2 =  win.p1.add("group");
win.p1.grp2.orientation='row';
win.p1.grp2.spacing=10;
win.p1.grp2.bu1 = win.p1.grp2.add('button',undefined,'Select');
win.p1.grp2.bu1.preferredSize=[190,25];
win.p1.grp2.bu2 = win.p1.grp2.add('button',undefined,'Cancel');
win.p1.grp2.bu2.preferredSize=[190,25];
win.p1.grp2.bu1.onClick = function(){
    if(win.p1.grp1.et1.text == ''){
        alert('No Layer Name Has Been Entered!');
        win.p1.grp1.et1.active=true;
        return;
        }
        win.close(1);
        process();
   }
win.p1.grp2.bu2.onClick=function(){
        win.close(2);
}
win.center();
win.show();
function process(){

if(win.p1.grp0.rb1.value){
var re = new RegExp (win.p1.grp1.et1.text.toString());
}else{
    var re = new RegExp ("^"+win.p1.grp1.et1.text.toString(),"i");
    }
var matches = collectNamesAM(re);
if(!matches.length){
    alert('Layer \"' + win.p1.grp1.et1.text.toString()+ '\" was not found!');
    return;
    }
if(win.p1.grp0.cb1.value){
if(activeDocument.layerSets.length > 1){
for(var a in matches){
   makeActiveByIndex(matches[a], false );
    }
}
for( var l = 0; l < matches.length; l++ ){
    if(l == 0) {
        makeActiveByIndex(matches[l], false );
        }else{
            makeActiveByIndex(matches[l], false,true );
            }  
 }
}else{
    if(matches.length == 1) {
        makeActiveByIndex(matches[0], false );
        }else{
 var win2 = new Window("dialog");
g = win2.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win2.title = win2.add('statictext',undefined,'LayerName Find');
var g = win2.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win2.p1  = win2.add("panel", undefined, undefined, {borderStyle:"black"});
win2.p1.grp0 =  win2.p1.add("group");
win2.p1.grp0.orientation='row';
win2.p1.grp0.alignChildren='fill';
win2.p1.grp0.spacing=10;
win2.p1.grp0.st1 = win2.p1.grp0.add('statictext',undefined, 'Please select layer');
win2.p1.grp0.st1.graphics.font=ScriptUI.newFont("Georgia","BOLDITALIC",16);
win2.p1.grp0.dd1 = win2.p1.grp0.add('dropdownlist');
for(var a = 1; a< matches.length+1;a++){
    win2.p1.grp0.dd1.add('item', a);
}
win2.p1.grp0.dd1.selection=0;
win2.p1.grp2 =  win2.p1.add("group");
win2.p1.grp2.orientation='row';
win2.p1.grp2.spacing=10;
win2.p1.grp2.bu1 = win2.p1.grp2.add('button',undefined,'Select');
win2.p1.grp2.bu1.preferredSize=[100,25];
win2.p1.grp2.bu2 = win2.p1.grp2.add('button',undefined,'Cancel');
win2.p1.grp2.bu2.preferredSize=[100,25];
win2.p1.grp2.bu2.onClick=function(){
        win2.close(2);
}
win2.p1.grp2.bu1.onClick=function(){
    win2.close(1);
makeActiveByIndex(matches[Number(win2.p1.grp0.dd1.selection.index)], false );
}
win2.center();
win2.show();
}
    }
    }

function collectNamesAM(re){
     var allLayers = new Array();
   var startLoop = Number( !hasBackground() );
   var endLoop = getNumberOfLayer(); 
   for( var l = startLoop;l < endLoop; l++){
        while( !isValidActiveLayer( l ) ) {
            l++;
        }
        if( getLayerNameByIndex( l ).match(re) != null){
            if(getLayerNameByIndex( l ).length == win.p1.grp1.et1.text.toString().length){
            allLayers.push( l );
            }
        }
    }
     return allLayers;
};
}
function getActiveLayerIndex() {
     var ref = new ActionReference();
     ref.putProperty( 1349677170 , 1232366921 );
     ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
     var res = executeActionGet(ref).getInteger( 1232366921 ) - Number( hasBackground() );
     res == 4 ? res++:res;
     return res;   
}
function isValidActiveLayer( idx ) {
     var propName = stringIDToTypeID( 'layerSection' );
     var ref = new ActionReference(); 
     ref.putProperty( 1349677170 , propName);
     ref.putIndex( 1283027488, idx );
     var desc =  executeActionGet( ref );
     var type = desc.getEnumerationValue( propName );
     var res = typeIDToStringID( type ); 
     return res == 'layerSectionEnd' ? false:true;
};
function hasBackground(){
    var res = undefined;
    try{
        var ref = new ActionReference();
        ref.putProperty( 1349677170 , 1315774496); 
        ref.putIndex( 1283027488, 0 );
        executeActionGet(ref).getString(1315774496 );
        res = true;
    }catch(e){ res = false}
    return res;
};
function makeActiveByIndex( idx, forceVisible,add ){
    add == undefined ? add = false : add = true
     try{
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putIndex(charIDToTypeID( "Lyr " ), idx)
          desc.putReference( charIDToTypeID( "null" ), ref );
          if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
          desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible );
          executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
     }catch(e){ return -1;}
};
function getNumberOfLayer(){ 
var ref = new ActionReference(); 
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref); 
var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL")); 
return numberOfLayer; 
};
function getLayerNameByIndex( idx ) { 
    var ref = new ActionReference(); 
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " )); 
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));; 
};

main()

 

ThihasoeAuthor
Known Participant
November 7, 2023

Thank you so much Sir @Stephen Marsh