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
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
...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
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 );
Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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.