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

Select layer by similar name -- Script

Community Beginner ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

Hello, i have a PSD document with multiple layers, i need a script that select all layers with the word "Horizontal" in it´s name, and after that, merge them in one layer.

I am using ExtendScript Toolkit CS6

Screen Shot 2017-01-10 at 10.55.44.png

TOPICS
Actions and scripting

Views

1.9K

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

correct answers 1 Correct answer

Engaged , Jan 10, 2017 Jan 10, 2017

Hi Ballestero,

 

Hope this help you...

 

cTID = function(s) {return app.charIDToTypeID(s);};

sTID = function(s) {return app.stringIDToTypeID(s);};     

if(app.documents.length>0){

    var docRef = app.activeDocument;

    docRef.activeLayer=docRef.backgroundLayer;

    var myLyr = docRef.artLayers;

        for (var i = 0; i < myLyr.length; i++) {

            if(myLyr[i].name.indexOf("Layer")!=-1){

                addtoSelection(myLyr[i].name);

            }

        }

}

function addtoSelection(lyNa

...

Votes

Translate

Translate
Adobe
Engaged ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

Hi Ballestero,

 

Hope this help you...

 

cTID = function(s) {return app.charIDToTypeID(s);};

sTID = function(s) {return app.stringIDToTypeID(s);};     

if(app.documents.length>0){

    var docRef = app.activeDocument;

    docRef.activeLayer=docRef.backgroundLayer;

    var myLyr = docRef.artLayers;

        for (var i = 0; i < myLyr.length; i++) {

            if(myLyr[i].name.indexOf("Layer")!=-1){

                addtoSelection(myLyr[i].name);

            }

        }

}

function addtoSelection(lyName){

    var desc71 = new ActionDescriptor();

    var ref65 = new ActionReference();

    ref65.putName( cTID( "Lyr " ), lyName );

    desc71.putReference( cTID( "null" ), ref65 );

    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ));

    desc71.putBoolean( cTID( "MkVs" ), false );

    executeAction( cTID( "slct" ), desc71, DialogModes.NO );

}

 

-yajiv

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 Beginner ,
Jan 11, 2017 Jan 11, 2017

Copy link to clipboard

Copied

Thank you very much, even, using your answer, i finished the code in order to exclude "Background" layer from selection and merge the layers in one single layer named "Horizontal":

 

Here is the code:

 

  cTID = function(s) {return app.charIDToTypeID(s);}; 
  sTID = function(s) {return app.stringIDToTypeID(s);};    
  if(app.documents.length>0){ 
    var docRef = app.activeDocument; 
    docRef.activeLayer=docRef.backgroundLayer; 
    var myLyr = docRef.artLayers; 
        for (var i = 0; i < myLyr.length; i++) { 
            if(myLyr[i].name.indexOf("Vertical")!=-1){ 
                addtoSelection(myLyr[i].name); 
           
       
 
  function addtoSelection(lyName){ 
    var desc71 = new ActionDescriptor(); 
    var ref65 = new ActionReference(); 
    ref65.putName( cTID( "Lyr " ), lyName ); 
    desc71.putReference( cTID( "null" ), ref65 ); 
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" )); 
    desc71.putBoolean( cTID( "MkVs" ), false ); 
    executeAction( cTID( "slct" ), desc71, DialogModes.NO ); 
 

 

var idslct = charIDToTypeID( "slct" );

  var desc14 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Background" );
  desc14.putReference( idnull, ref4 );
  var idselectionModifier = stringIDToTypeID( "selectionModifier" );
  var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
  var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
  desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
  var idMkVs = charIDToTypeID( "MkVs" );
  desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

 

 

var idMk = charIDToTypeID( "Mk  " );

  var desc22 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref7 = new ActionReference();
    var idlayerSection = stringIDToTypeID( "layerSection" );
    ref7.putClass( idlayerSection );
  desc22.putReference( idnull, ref7 );
  var idFrom = charIDToTypeID( "From" );
    var ref8 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref8.putEnumerated( idLyr, idOrdn, idTrgt );
  desc22.putReference( idFrom, ref8 );

executeAction( idMk, desc22, DialogModes.NO );

 

// =======================================================

var idsetd = charIDToTypeID( "setd" );

  var desc23 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref9.putEnumerated( idLyr, idOrdn, idTrgt );
  desc23.putReference( idnull, ref9 );
  var idT = charIDToTypeID( "T   " );
    var desc24 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc24.putString( idNm, """Vertical""" );
  var idLyr = charIDToTypeID( "Lyr " );
  desc23.putObject( idT, idLyr, desc24 );

executeAction( idsetd, desc23, DialogModes.NO );

 

// =======================================================

var idMrgtwo = charIDToTypeID( "Mrg2" );

executeAction( idMrgtwo, undefined, DialogModes.NO );

 

 

///////////////////////////////////

 

 

  cTID = function(s) {return app.charIDToTypeID(s);}; 
  sTID = function(s) {return app.stringIDToTypeID(s);};    
  if(app.documents.length>0){ 
    var docRef = app.activeDocument; 
    docRef.activeLayer=docRef.backgroundLayer; 
    var myLyr = docRef.artLayers; 
        for (var i = 0; i < myLyr.length; i++) { 
            if(myLyr[i].name.indexOf("Horizontal")!=-1){ 
                addtoSelection(myLyr[i].name); 
           
       
 
  function addtoSelection(lyName){ 
    var desc71 = new ActionDescriptor(); 
    var ref65 = new ActionReference(); 
    ref65.putName( cTID( "Lyr " ), lyName ); 
    desc71.putReference( cTID( "null" ), ref65 ); 
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" )); 
    desc71.putBoolean( cTID( "MkVs" ), false ); 
    executeAction( cTID( "slct" ), desc71, DialogModes.NO ); 
 

 

var idslct = charIDToTypeID( "slct" );

  var desc14 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Background" );
  desc14.putReference( idnull, ref4 );
  var idselectionModifier = stringIDToTypeID( "selectionModifier" );
  var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
  var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
  desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
  var idMkVs = charIDToTypeID( "MkVs" );
  desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

 

var idslct = charIDToTypeID( "slct" );

  var desc14 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Vertical" );
  desc14.putReference( idnull, ref4 );
  var idselectionModifier = stringIDToTypeID( "selectionModifier" );
  var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
  var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
  desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
  var idMkVs = charIDToTypeID( "MkVs" );
  desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

 

 

var idMk = charIDToTypeID( "Mk  " );

  var desc22 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref7 = new ActionReference();
    var idlayerSection = stringIDToTypeID( "layerSection" );
    ref7.putClass( idlayerSection );
  desc22.putReference( idnull, ref7 );
  var idFrom = charIDToTypeID( "From" );
    var ref8 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref8.putEnumerated( idLyr, idOrdn, idTrgt );
  desc22.putReference( idFrom, ref8 );

executeAction( idMk, desc22, DialogModes.NO );

 

// =======================================================

var idsetd = charIDToTypeID( "setd" );

  var desc23 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref9.putEnumerated( idLyr, idOrdn, idTrgt );
  desc23.putReference( idnull, ref9 );
  var idT = charIDToTypeID( "T   " );
    var desc24 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc24.putString( idNm, """Horizontal""" );
  var idLyr = charIDToTypeID( "Lyr " );
  desc23.putObject( idT, idLyr, desc24 );

executeAction( idsetd, desc23, DialogModes.NO );

 

// =======================================================

var idMrgtwo = charIDToTypeID( "Mrg2" );

executeAction( idMrgtwo, undefined, DialogModes.NO );

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 ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

Changes to the forum software messed up old code:

 

Select all layers with same name script

 

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
Explorer ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

Hi,

 

I have found the code from the following link Select layer by similar name -- Script that selects all layers with the same name:

 

 

cTID = function(s) {return app.charIDToTypeID(s);};
sTID = function(s) {return app.stringIDToTypeID(s);};      
if(app.documents.length>0){
    var docRef = app.activeDocument;
    docRef.activeLayer=docRef.backgroundLayer;
    var myLyr = docRef.artLayers;
        for (var i = 0; i < myLyr.length; i++) {
            if(myLyr.name.indexOf("Layer")!=-1){
                addtoSelection(myLyr.name);
            }
        }
}
function addtoSelection(lyName){
    var desc71 = new ActionDescriptor();
    var ref65 = new ActionReference();
    ref65.putName( cTID( "Lyr " ), lyName );
    desc71.putReference( cTID( "null" ), ref65 );
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ));
    desc71.putBoolean( cTID( "MkVs" ), false );
    executeAction( cTID( "slct" ), desc71, DialogModes.NO );
}

 

 

The code is stopping at line 8 and throwing the following error:

Error 1302: No such element

Line 8:

->      if(myLyr.name.indexOf("Layer")!=-1){

 

Why is this error showing and how to fix it?

My simple question is how to search and select all layers with the same name by a script.

 

Thank you.

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
People's Champ ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

Replace in two places

 

myLyr.name

 

with

 

myLyr[i].name

 

upd.

>>>My simple question is how to search and select all layers with the same name by a script.

But it is not clear what exactly and how you want

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
Explorer ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

LATEST

Thank you r-bin, this solves the problem. I will use this code to achieve certain selections of many layers with the same name and then will apply certain filter on them.

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