• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to search layer names

New Here ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

Hi -

Does anyone know if there is a script that can search through layer names within a file for a specific text string and then select it as the active layer?

For instance, if a file has 4 layers, named My LayerX, LayerY, LayerZ, LayerA, I want to automatically search for a layer that has the text "rA" and make it the active layer.

Thanks.

TOPICS
Actions and scripting

Views

17.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 08, 2015 Oct 08, 2015

Copy link to clipboard

Copied

hi There is any same kind of scripts available for Adobe illustrator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2015 Oct 08, 2015

Copy link to clipboard

Copied

I do not use AI one complex Adobe prouduct Photoshop is all I can handle.

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

This requires CS4 or better. It searchs using a RegExp and Action Manager so it should be very fast. It is set up now to deal with more than one matching layer.

var re = /rA/;// a reg exp
var matches = collectNamesAM(re);// get array of matching layer indexes
for( var l = 0; l < matches.length; l++ ){
    makeActiveByIndex( l, false );
    // do something with layer
    alert(app.activeDocument.activeLayer.name);
}// next match if any

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){
            allLayers.push( l );
        }
    }
     return allLayers;
};
/*//////////////////////////////////////////////////////////////////////////////
// Function: getActiveLayerIndex
// Description: Gets gets the Action Manager API index
//                        of activeLayer corrected for Background Layer
// Usage: var idx = getActiveLayerIndex();
// Input: None
// Return: Number - correct AM itemIndex
// Dependices: hasBackground
//////////////////////////////////////////////////////////////////////////////*/
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// why the skip in this doc?
     return res;  
}
/*//////////////////////////////////////////////////////////////////////////////
// Function: isValidActiveLayer( )
// Description: Checks LayerSection for 'real' layers
// Usage: if( isValidActiveLayer() )
// Input: None
// Return: Boolean - True if not the end of a Set
// Notes:  Needed only if the layer was made active
//               using Action Manager API
//////////////////////////////////////////////////////////////////////////////*/
function isValidActiveLayer( idx ) {
     var propName = stringIDToTypeID( 'layerSection' );// can't replace
     var ref = new ActionReference();
     ref.putProperty( 1349677170 , propName);// TypeID for "Prpr"
     // 'Lyr ", idx
     ref.putIndex( 1283027488, idx );
     var desc =  executeActionGet( ref );
     var type = desc.getEnumerationValue( propName );
     var res = typeIDToStringID( type );
     return res == 'layerSectionEnd' ? false:true;
};
/*//////////////////////////////////////////////////////////////////////////////
// Function: hasBackground
// Description: Test for background layer using AM API
// Usage: if( hasBackground() );
// Input: None
// Return: Boolean - true if doc has background layer
// Notes:  Requires the document to be active
//  DOM:  App.Document.backgroundLayer
//////////////////////////////////////////////////////////////////////////////*/
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 ){
     try{
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putIndex(charIDToTypeID( "Lyr " ), idx)
          desc.putReference( charIDToTypeID( "null" ), ref );
          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  " ));;
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 09, 2014 Aug 09, 2014

Copy link to clipboard

Copied

@Michael: Thank you for the awesome code!

Annotation: line 4 must be the following:

var re = /rA/;// a reg exp 

var matches = collectNamesAM(re);// get array of matching layer indexes 

for( var l = 0; l < matches.length; l++ ){ 

    makeActiveByIndex( matches, false ); 

    // do something with layer 

    alert(app.activeDocument.activeLayer.name); 

}// next match if any 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jul 19, 2016 Jul 19, 2016

Copy link to clipboard

Copied

Micheal what do you mean it requires Action Manager, I tried to execute after doing the change barra put forward and nothing happened ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2016 Jul 19, 2016

Copy link to clipboard

Copied

Michael has passed away. Action Manger code is code generated by Scriptlistener - for the most part as opposed to DOM code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2016 Jul 19, 2016

Copy link to clipboard

Copied

LATEST

The functions in Michael script were made from Action Manager Coder recorded by the Scriptlistener Plug-in in some of the functions variable were added to replace hard coded values recorded by the Scriptlistener plug-in the variable are the functions parameters. I have highlighted the variable and the statment's that  uses Photoshop Action manager

  1. function getActiveLayerIndex() { 
  2.      var ref = new ActionReference(); 
  3.      ref.putProperty( 1349677170 , 1232366921 ); 
  4.      ref.putEnumerated( 1283027488, 1332896878, 1416783732 ); 
  5.      var res = executeActionGet(ref).getInteger( 1232366921 ) 
  6.                                                        - Number( hasBackground() ); 
  7.      res == 4 ? res++:res// why the skip in this doc? 
  8.      return res;    
  9. /*////////////////////////////////////////////////////////////////////////////// 
  10. // Function: isValidActiveLayer( ) 
  11. // Description: Checks LayerSection for 'real' layers 
  12. // Usage: if( isValidActiveLayer() ) 
  13. // Input: None 
  14. // Return: Boolean - True if not the end of a Set 
  15. // Notes:  Needed only if the layer was made active 
  16. //               using Action Manager API 
  17. //////////////////////////////////////////////////////////////////////////////*/ 
  18. function isValidActiveLayer( idx ) { 
  19.      var propName = stringIDToTypeID( 'layerSection' );// can't replace 
  20.      var ref = new ActionReference();  
  21.      ref.putProperty( 1349677170 , propName);// TypeID for "Prpr" 
  22.      // 'Lyr ", idx 
  23.      ref.putIndex( 1283027488, idx ); 
  24.      var desc =  executeActionGet( ref ); 
  25.      var type = desc.getEnumerationValue( propName ); 
  26.      var res = typeIDToStringID( type );  
  27.      return res == 'layerSectionEnd' ? false:true; 
  28. }; 
  29. /*////////////////////////////////////////////////////////////////////////////// 
  30. // Function: hasBackground 
  31. // Description: Test for background layer using AM API 
  32. // Usage: if( hasBackground() ); 
  33. // Input: None 
  34. // Return: Boolean - true if doc has background layer 
  35. // Notes:  Requires the document to be active 
  36. //  DOM:  App.Document.backgroundLayer 
  37. //////////////////////////////////////////////////////////////////////////////*/ 
  38. function hasBackground(){ 
  39.     var res = undefined; 
  40.     try{ 
  41.         var ref = new ActionReference(); 
  42.         ref.putProperty( 1349677170 , 1315774496);  
  43.         ref.putIndex( 1283027488, 0 ); 
  44.         executeActionGet(ref).getString(1315774496 );;  
  45.         res = true; 
  46.     }catch(e){ res = false} 
  47.     return res; 
  48. }; 
  49. function makeActiveByIndex( idx, forceVisible ){ 
  50.      try{ 
  51.           var desc = new ActionDescriptor(); 
  52.           var ref = new ActionReference(); 
  53.           ref.putIndex(charIDToTypeID( "Lyr " ), idx
  54.           desc.putReference( charIDToTypeID( "null" ), ref ); 
  55.           desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible ); 
  56.           executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
  57.      }catch(e){ return -1;} 
  58. }; 
  59. function getNumberOfLayer(){  
  60. var ref = new ActionReference();  
  61. ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
  62. var desc = executeActionGet(ref);  
  63. var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL"));  
  64. return numberOfLayer;  
  65. }; 
  66. function getLayerNameByIndex( idx ) {  
  67.     var ref = new ActionReference();  
  68.     ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));  
  69.     ref.putIndex( charIDToTypeID( "Lyr " ), idx ); 
  70.     return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;  
  71. }; 
JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines