Skip to main content
Known Participant
July 14, 2021
Answered

Select Next Layer in Script

  • July 14, 2021
  • 1 reply
  • 1492 views

In this pic I have Selected Layer2 or Layer 5, I want to Select Next Layer 1 or Layer 5 ?

In other words Multiple Layer Select , And Multiple "Select Next Layer" ?

Can you modify the script ?

Thanks in advance !

nextLayer('down');

// direction (↑): "up" or "above"
// direction (↓): "down" or "below"
function nextLayer( direction ) {
  
  var doc = app.activeDocument;
   // Doc duplication is necessary because while the History panel can record visibility change, but for some reason it doesn't do that when the visibility command comes from a script... (AFAIK)
  var tempDoc = doc.duplicate();
  var layer1 = tempDoc.activeLayer;
  
  // Turn background layer into a normal layer
  var lastLayer = tempDoc.layers[ tempDoc.layers.length-1 ];
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };
  var layer1ID = activeLayerID();
  tempDoc.activeLayer = lastLayer;
  var bgLayerExists = lastLayer.isBackgroundLayer;
  if ( bgLayerExists ) { lastLayer.isBackgroundLayer = false; }
  try { selectLayerByID( layer1ID ); } catch(e) {}
  // Select all layers
  var desc23 = new ActionDescriptor();
  var ref5 = new ActionReference();
  ref5.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
  desc23.putReference( cTID('null'), ref5 );
  executeAction( sTID('selectAllLayers'), desc23, DialogModes.NO );
  // Make active layers visible
  var desc209 = new ActionDescriptor();
  var list93 = new ActionList();
  var ref129 = new ActionReference();
  ref129.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
  list93.putReference( ref129 );
  desc209.putList( cTID('null'), list93 );
  executeAction( cTID('Shw '), desc209, DialogModes.NO );
  // Reselect the starting layer
  if ( bgLayerExists ) { lastLayer.isBackgroundLayer = true; }
  selectLayerByID( layer1ID );
  // Select next layer
  try { snl( direction ); } catch(e) {}
  // Store the layer
  var nextLayer = activeLayerID();
  tempDoc.close( SaveOptions.DONOTSAVECHANGES );
  // Try to select the next layer using its ID
  try {
    selectLayerByID( nextLayer );
  }
  // If it fails, well assume it did so because it was a background layer... and use another method for selecting that.
  catch(e) {
    var desc299 = new ActionDescriptor();
        var ref187 = new ActionReference();
        ref187.putName( cTID('Lyr '), "Background" );
    desc299.putReference( cTID('null'), ref187 );
    desc299.putBoolean( cTID('MkVs'), false );
        var list138 = new ActionList();
        list138.putInteger( 1 );
    desc299.putList( cTID('LyrI'), list138 );
    executeAction( cTID('slct'), desc299, DialogModes.NO );
  }
  
  function snl( direction ) {
    
    var select;
    if ( direction == 'up' || direction == 'above' ) {
      select = cTID('Frwr');
    }
    else if ( direction == 'down' || direction == 'below' ) {
      select = cTID('Bckw');
    }
    
    var desc67 = new ActionDescriptor();
    var ref41 = new ActionReference();
    ref41.putEnumerated( cTID('Lyr '), cTID('Ordn'), select );
    desc67.putReference( cTID('null'), ref41 );
    desc67.putBoolean( cTID('MkVs'), false );
    var list17 = new ActionList();
    list17.putInteger( 5 );
    desc67.putList( cTID('LyrI'), list17 );
    executeAction( cTID('slct'), desc67, DialogModes.NO );
  }
  
  function activeLayerID() {
      var ref = new ActionReference();
      ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
      ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
  	return executeActionGet(ref).getInteger( stringIDToTypeID( "layerID" ) );
  }
  
  function selectLayerByID(id, add){
     add = (add == undefined) ? add = false : add;
     var ref = new ActionReference();
     ref.putIdentifier(cTID('Lyr '), id);
     var desc = new ActionDescriptor();
     desc.putReference(cTID('null'), ref);
     if(add){
        desc.putEnumerated(sTID('selectionModifier'), sTID('selectionModifierType'), sTID('addToSelection'));
     }
     desc.putBoolean(cTID('MkVs'), false);
     executeAction(cTID('slct'), desc, DialogModes.NO);
  }
  
}
This topic has been closed for replies.
Correct answer r-bin

Your original code can be simplified

 

var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("backwardEnum"));
d.putReference(stringIDToTypeID("null"), r);
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

 

For multiple selected layers try this code

 

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var list = executeActionGet(r).getList(stringIDToTypeID("targetLayers"));

var r_idx = new ActionReference();

for (var i = 0; i < list.count; i++) r_idx.putIndex(stringIDToTypeID("layer"), list.getReference(i).getIndex()-1); // +1 to select up

var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r_idx);
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

 

1 reply

r-binCorrect answer
Legend
July 14, 2021

Your original code can be simplified

 

var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("backwardEnum"));
d.putReference(stringIDToTypeID("null"), r);
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

 

For multiple selected layers try this code

 

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var list = executeActionGet(r).getList(stringIDToTypeID("targetLayers"));

var r_idx = new ActionReference();

for (var i = 0; i < list.count; i++) r_idx.putIndex(stringIDToTypeID("layer"), list.getReference(i).getIndex()-1); // +1 to select up

var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r_idx);
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

 

MXKSAuthor
Known Participant
July 15, 2021

Thanku so much @r-bin  Worked It 

Legend
July 15, 2021

: )

Not tested when reaching the end of the layer stack, or when folders are present.