Copy link to clipboard
Copied
Hello,
I want to write a script which creates a new layer.
I'm using app.aciveDocument.artLayers.add() method, however it creates the new layer at the top of the layer list.
Is there any way to create the layer just above the active layer?
Thanks
You can use this method
var layer0 = app.activeDocument.activeLayer;
app.activeDocument.artLayers.add();
app.activeDocument.activeLayer.move(layer0, ElementPlacement.PLACEBEFORE);
Copy link to clipboard
Copied
// mode_str = "Nrml"; "Lghn", "Drkn", "Nrml", "Clr ", "Lmns", etc...
////////////////////////////////////////////////////////////////////////////////////////////
function add_layer(name, op, mode_str, grp)
{
try {
var desc3 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putClass( charIDToTypeID( "Lyr " ) );
desc3.putReference( charIDToTypeID( "null" ), ref1 );
var desc4 = new ActionDescriptor();
if (name != undefined) desc4.putString( charIDToTypeID( "Nm " ), name);
if (op != undefined) desc4.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), op );
if (grp != undefined) desc4.putBoolean( charIDToTypeID( "Grup" ), grp );
if (mode_str != undefined)
{
var mode = (mode_str.length==4) ? charIDToTypeID(mode_str) : stringIDToTypeID(mode_str);
desc4.putEnumerated( charIDToTypeID( "Md " ), charIDToTypeID( "BlnM" ), mode );
}
desc3.putObject( charIDToTypeID( "Usng" ), charIDToTypeID( "Lyr " ), desc4 );
executeAction( charIDToTypeID( "Mk " ), desc3, DialogModes.NO );
ref1 = null;
desc3 = null;
desc4 = null;
}
catch(e) { alert(e); throw(e); }
}
Copy link to clipboard
Copied
If I understood correctly, this should work.
make();
function make() {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putClass( stringIDToTypeID( "layer" ));
descriptor.putReference( charIDToTypeID( "null" ), reference );
descriptor.putObject( stringIDToTypeID( "using" ), stringIDToTypeID( "layer" ), descriptor2 );
executeAction( stringIDToTypeID( "make" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
Thank you guys for your replies.
I was hoping to be able achieve this using methods as per Adobe's JavaScript Scripting Reference.
Nevertheless thank you very much, really appreciated.
Copy link to clipboard
Copied
You can use this method
var layer0 = app.activeDocument.activeLayer;
app.activeDocument.artLayers.add();
app.activeDocument.activeLayer.move(layer0, ElementPlacement.PLACEBEFORE);
Copy link to clipboard
Copied
r-bin Thank you!