How to refer a layer by it's order number?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

xaero

How to refer a layer by it's order number?

Post by xaero »

Code: Select allvar idslct = charIDToTypeID( "slct" );
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref3.putName( idLyr, "Background" );
    desc5.putReference( idnull, ref3 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc5.putBoolean( idMkVs, false );
executeAction( idslct, desc5, DialogModes.NO );

I think the following code refer a layer by it's layer name, sometimes the name of the first layer is not "background" but some other name.
ref3.putName( idLyr, "Background" );

how to change the code?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

How to refer a layer by it's order number?

Post by Mike Hale »

Not sure what you are asking.

If you want to select layer by name you can replace "Background" with the name you want. i.e. ref3.putName( idLyr, "Layer 1" ); Or you can use the Photoshop Object Model version app.activeDocument.layers.getByName("Layer 1");

If you want the bottom layer regardless of it's name you can use app.activeDocument.layers[app.activeDocument.layers.length-1];
xaero

How to refer a layer by it's order number?

Post by xaero »

Yes! this code will work fine:
app.activeDocument.layers[app.activeDocument.layers.length-1];
but how to modify this line:
ref3.putName( idLyr, "Background" );
"Background" should be the bottom layer , not the layer named "Background".
txuku

How to refer a layer by it's order number?

Post by txuku »

Bonjour

You can use this line instead your script :
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
Mikaeru

How to refer a layer by it's order number?

Post by Mikaeru »

xaero wrote:how to modify this line:
ref3.putName( idLyr, "Background" );
"Background" should be the bottom layer , not the layer named "Background".
In order to explicitely select the background layer, you can use the selectBackgroundLayer ( ) function listed below:

Code: Select allfunction selectBackgroundLayer (makeVisible)
{
   var desc = new ActionDescriptor ();
   var ref = new ActionReference ();
   ref.putProperty (stringIDToTypeID ("layer"), stringIDToTypeID ("background"));
   desc.putReference (stringIDToTypeID ("target"), ref);
   desc.putBoolean (stringIDToTypeID ("makeVisible"), makeVisible);
   executeAction (stringIDToTypeID ("select"), desc, DialogModes.NO);
}

function selectLayerByName (layerName, makeVisible)
{
   var desc = new ActionDescriptor ();
   var ref = new ActionReference ();
   ref.putName (stringIDToTypeID ("layer"), layerName);
   desc.putReference (stringIDToTypeID ("target"), ref);
   desc.putBoolean (stringIDToTypeID ("makeVisible"), makeVisible);
   executeAction (stringIDToTypeID ("select"), desc, DialogModes.NO);
}

// Examples
selectBackgroundLayer (true);
selectLayerByName ("Layer 1", false);

or, using the DOM:

Code: Select allapp.activeDocument.activeLayer = app.activeDocument.backgroundLayer;
Mike Hale

How to refer a layer by it's order number?

Post by Mike Hale »

I am still not clear about what you are asking. The layer Photoshop considers the background layer is always named 'Background'. One way to change a background layer into an artLayer is by renaming. On the other hand if you convert an artLayer to a background layer Photoshop will rename that layer "Background.

The bottom layer in the layer's panel will always have an index of the layers.length-1. If that bottom layer is named "Background" in italic and the layer is locked, it's the background layer. If the name is not in italic, it's an artLayer even if the name is "Background"

Because Photoshop treats the background layer different than other layers it has a special index in Action Manager. You can reference the background layer by the index of zero. ref3.putIndex( idLyr, 0 ); However if the document doesn't have a background layer that will cause an error because only the background layer is 0. If the bottom layer is an artlLayer the index is 1.
xaero

How to refer a layer by it's order number?

Post by xaero »

Thanks a lot, Mike. And sorry for my poor English.

this tip help me a lot:
Because Photoshop treats the background layer different than other layers it has a special index in Action Manager. You can reference the background layer by the index of zero. ref3.putIndex( idLyr, 0 ); However if the document doesn't have a background layer that will cause an error because only the background layer is 0. If the bottom layer is an artlLayer the index is 1.
Thanks!

In fact, I want to reference the "Background" layer and the layer just above the background layer.
In Chinese version of photoshop, the background is named "背景", and in English version it is "Background".
I know the the index of background is zero, but I don't know how to write the code.
Mike Hale

How to refer a layer by it's order number?

Post by Mike Hale »

You can use mikreru's function by calling it like thisCode: Select allselectBackgroundLayer (false);Or you can change the putName to putIndex as I suggestedCode: Select all    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID( "Lyr " ), 0 );
    desc.putReference( charIDToTypeID( "null" ), ref );
    desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );Or select the bottom layer regardless of typeCode: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];Then check to see if it's the special background layer withCode: Select allapp.activeDocument.activeLayer.isBackgroundLayer

Am forgive me for assuming that the background layer is named "Background" in every language version.
xaero

How to refer a layer by it's order number?

Post by xaero »

Thank you very much