Photoshop: How do I get active layers in script?

  • 1
  • Question
  • Updated 2 years ago
  • (Edited)
I am new to Photoshop scripting. So please forgive me...

Do I really have to do:

function getSelectedLayers() { 
      var A = []; 
      var desc11 = new ActionDescriptor(); 
      var ref9 = new ActionReference(); 
      desc11.putReference(charIDToTypeID('null'), ref9); 
      var ref10 = new ActionReference(); 
      ref10.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); 
      desc11.putReference(charIDToTypeID('From'), ref10); 
      executeAction(charIDToTypeID('Mk  '), desc11, DialogModes.NO); 
      var gL = activeDocument.activeLayer.layers; 
      for (var i = 0; i < gL.length; i++) { 
      executeAction(charIDToTypeID('undo'), undefined, DialogModes.NO); 
      return A; 

to get a list of the active layers I can then process?

Or is there an easier way?

I know that I can do: var layerCount = app.activeDocument.artLayers.length; and I get the count of layers in a document.

Why can't I just do something like: var listOfActiveLayers = app.activeDocument.selectedLayers;

In general, does anybody have a good reference guide, or tutorials on Photoshop scripting? I find it hard to find things via Google. e.g. searching for: "get list active layers scripting photoshop" returns almost no result on Google. Maybe 4 - 5 on scripting and then it goes into general "how to create a layer in photshop".

Here's what I am looking for in terms of Maya MEL scripting reference:
Photo of Alex Furer

Alex Furer

  • 152 Posts
  • 40 Reply Likes

Posted 2 years ago

  • 1
Photo of Max Johnson

Max Johnson, Champion

  • 511 Posts
  • 255 Reply Likes
On my phone, but I saw a hack where someone scripted

group selected
var lyrs = layergroup.artLayers

Really fast list of artLayer objs.
Photo of Alex Furer

Alex Furer

  • 152 Posts
  • 40 Reply Likes
Thanks Max, But this didn't work for me. I tried it in any way, shape or form I could come up with. It kept reporting that there's a syntax error.
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 834 Posts
  • 201 Reply Likes
I think yes. If you will not use some library with premade functions.
Your example is Action Manager code. It has better coverage but is less readable.

DOM scripting is more friendly but has only basic things and is slower. With DOM you can get only one selected layer.

The Action Manager code has advantage that you don't need much obfuscation because it already looks like that :-D 

But believe or not AM code makes sense. It's like JSON but there are data types and you need everywhere use keys conversion stringIDToTypeID("keyName")=123456;

This could help you to understand AM code:
Photo of Alex Furer

Alex Furer

  • 152 Posts
  • 40 Reply Likes
Thanks Yaroslav, I will study your suggestion. I am not worried about speed as all I am trying to do is get a list of the selected layers and then rename them with some specific input values I get from a user dialog. And I am almost there. Just that the script I started from renames all layers in the document and I'd like to only process the selected ones, either from the top or from the bottom.

Problem is that I am not a programmer per-se. I can cobble together different pieces of code and make then do what I want. So I was a bit baffled by the fact that collection a list from the selected layers is so hard to do. In Maya, the user selection was the base of almost all operations that were then done by the script...
Photo of Max Johnson

Max Johnson, Champion

  • 511 Posts
  • 255 Reply Likes
I found the script I mentioned in an email... untested, but here's what it looks like:

var docRef = app.activeDocument;

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function newGroupFromLayers(doc) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass( sTID('layerSection') );
    desc.putReference( cTID('null'), ref );
    var lref = new ActionReference();
    lref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc.putReference( cTID('From'), lref);
    executeAction( cTID('Mk  '), desc, DialogModes.NO );
function undo() {
   executeAction(cTID("undo", undefined, DialogModes.NO));
function getSelectedLayers(doc) {
  var selLayers = [];
  var group = doc.activeLayer;
  var layers = group.layers;
  for (var i = 0; i < layers.length; i++) {
  return selLayers;
var selectedLayers = getSelectedLayers(app.activeDocument);
for( i = 0; i < selectedLayers.length; i++) {
    selectedLayers[i].selected = true;
    docRef.activeLayer = selectedLayers[i];
Photo of Alex Furer

Alex Furer

  • 152 Posts
  • 40 Reply Likes
Thanks Max! That works. Now I will try to do the rename dance with this.