How to select active layer?

Pablo picture Pablo · Mar 9, 2013 · Viewed 16.1k times · Source

It appears that in order to resize the layer it has to be selected(from UI perspective) and active from API perspective. Otherwise I get error on any function call that this function is not supported.

So before resizing I do

var a = doc.artLayers.getByName("iPad");
app.activeDocument.activeLayer = a;

This doesn't visually change selected layer hence calling resize function fails after that. The only way to get it work, manually click on layer(any layer), then it works. What is the proper way to resize layer without user interaction?

Answer

Ghoul Fool picture Ghoul Fool · Mar 11, 2013

You just need to amend your code:

var doc = app.activeDocument;
doc.activeLayer = doc.artLayers.getByName("iPad");

This will set the active layer to the one named "ipad". This is a standard way of selecting a layer (by name) to then further process the image, in your case resizing it. Obviously I don't know what else in in the PSD in terms of layers to pick or ignore. Another way would be to iterate through all layers and process them all.

Here are two useful functions: One will select the layer mask is there is one, the other will deselect the layer mask and go back to the bitmap layer

// FUNCTION DESELECT LAYER MASK AND SELECT IMAGE LAYER
// --------------------------------------------------------
function deselectLayerMaskAndSelectImageLayer()
{
  // =======================================================
  var id248 = charIDToTypeID( "slct" );
  var desc48 = new ActionDescriptor();
  var id249 = charIDToTypeID( "null" );
  var ref36 = new ActionReference();
  var id250 = charIDToTypeID( "Chnl" );
  var id251 = charIDToTypeID( "Chnl" );
  var id252 = charIDToTypeID( "RGB " );
  ref36.putEnumerated( id250, id251, id252 );
  desc48.putReference( id249, ref36 );
  var id253 = charIDToTypeID( "MkVs" );
  desc48.putBoolean( id253, false );
  executeAction( id248, desc48, DialogModes.NO );
}


// FUNCTION SELECT MASK
// --------------------------------------------------------
function selectMask(LayerName)
{
  try
  {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    ref.putName( charIDToTypeID('Lyr '), LayerName );
    desc.putReference( charIDToTypeID('null'), ref );
    desc.putBoolean( charIDToTypeID('MkVs'), true );
    executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );

    // =======================================================
    var id1083 = charIDToTypeID( "setd" );
    var desc238 = new ActionDescriptor();
    var id1084 = charIDToTypeID( "null" );
    var ref161 = new ActionReference();
    var id1085 = charIDToTypeID( "Chnl" );
    var id1086 = charIDToTypeID( "fsel" );
    ref161.putProperty( id1085, id1086 );
    desc238.putReference( id1084, ref161 );
    var id1087 = charIDToTypeID( "T   " );
    var ref162 = new ActionReference();
    var id1088 = charIDToTypeID( "Chnl" );
    var id1089 = charIDToTypeID( "Ordn" );
    var id1090 = charIDToTypeID( "Trgt" );
    ref162.putEnumerated( id1088, id1089, id1090 );
    desc238.putReference( id1087, ref162 );
    executeAction( id1083, desc238, DialogModes.NO );
  }
  catch(e)
  {
  //alert(e)
  //alert( "This layer has NO layer mask!" );
  activeDocument.selection.deselect();
  }
} //end function