PS Script - how to rename layer without selecting it?

  • 1
  • Question
  • Updated 2 years ago
How to rename layer without selecting the layer?
Now I am using
app.activeDocument.activeLayer.name = myVariable;
I can't use
app.activeDocument.artLayers[myIndex].name = myVariable;
Because layers are nested in groups and sometimes I need rename group

This code rename selected layer:
var idsetd = charIDToTypeID( "setd" );
    var desc491 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref264 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref264.putEnumerated( idLyr, idOrdn, idTrgt );
    desc491.putReference( idnull, ref264 );
    var idT = charIDToTypeID( "T   " );
        var desc492 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc492.putString( idNm, """test""" );
    var idLyr = charIDToTypeID( "Lyr " );
    desc491.putObject( idT, idLyr, desc492 );
executeAction( idsetd, desc491, DialogModes.NO );
How to modify this code to change layer with specific "id" or "itemIndex" ? "Trgt" shloud give me list of itemIndex. I need replace "Trgt" with my index number.
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes

Posted 2 years ago

  • 1
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes
I found answer here: http://www.ps-bridge-scripts.talktalk.net/layerIdIdx.htm
var idsetd = charIDToTypeID( "setd" );
    var desc491 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref264 = new ActionReference();
        ref264.putIndex( charIDToTypeID( "Lyr " ), 4 ); // <-- 4 = my index number
    desc491.putReference( idnull, ref264 );
    var idT = charIDToTypeID( "T   " );
        var desc492 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc492.putString( idNm, """test""" );
    var idLyr = charIDToTypeID( "Lyr " );
    desc491.putObject( idT, idLyr, desc492 );
executeAction( idsetd, desc491, DialogModes.NO );
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes
This works only for selected layer.
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
function putLayerNameByIndex( idx, name ) {
if( idx == 0 ) return;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), idx );
//ref.putIdentifier(charIDToTypeID('Lyr '), ID); //Layer ID
desc.putReference( charIDToTypeID('null'), ref );
var nameDesc = new ActionDescriptor();
nameDesc.putString( charIDToTypeID('Nm '), name );
desc.putObject( charIDToTypeID('T '), charIDToTypeID('Lyr '), nameDesc );
executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes
It works only with selected layer in CC too. Btw your scipt makes hidden layers visible. Anyway thanks for code. Maybe I could reuse this code. Perhaps something else doesn't need active layer.
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
Add this line before the select.
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes
I tried to reuse this code for layer moving, but also works only with selected layer. And "layer.translate()" is slower and it's hard to find if layer is inside groups.
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
There is no easy or one way to do things.
Here is some code I wrote to move a layer to inside a layerset.
Notice I need to change the ID to an Index.

function moveLayerToLayerSet( fromID, toID ){
var desc5 = new ActionDescriptor();
var ref4 = new ActionReference();
ref4.putIdentifier( charIDToTypeID('Lyr '), Number(fromID) );
desc5.putReference( charIDToTypeID('null'), ref4 );
var ref5 = new ActionReference();
ref5.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(toID) );
desc5.putReference( charIDToTypeID('T   '), ref5 );
desc5.putBoolean( charIDToTypeID('Adjs'), false );
desc5.putInteger( charIDToTypeID('Vrsn'), 5 );
try{
executeAction( charIDToTypeID('move'), desc5, DialogModes.NO );
}catch(e){alert(e);}
};

function getLayerIndexByID(ID){
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID('Lyr '), ID );
try{
activeDocument.backgroundLayer;
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;
}catch(e){
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
}
};
(Edited)
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 809 Posts
  • 187 Reply Likes
Thank you for the code, but I mean the move in the canvas. Not move in the layers order.  Anyway, this too could be useful.