Skip to main content
Known Participant
October 21, 2019
Answered

Script for selecting layers / files from the drop-down menu.

  • October 21, 2019
  • 2 replies
  • 1386 views

Good night. Tell me, please.
How to make a script so that in the drop-down menu you can select the name of the layers of the current document? Second script: so that in the drop-down menu you can select the name of the files in the previously specified folder.
Thanks.

This topic has been closed for replies.
Correct answer Chuck Uebele

I'm not sure what you mean by previous specified folder. Is that the folder where the current active document is located?

Here's a script for listing the layers:

#target photoshop
var doc = activeDocument;
    var layerList = [];
    var idxList = [];
    var layerSets = 0
    try{doc.backgroundLayer}
    catch(e){layerSets=1}
    var loopStart = layerSets;
    var layerListFirst = getLayerSetsData();  
    var r = new ActionReference();
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayersIndexes"));
    r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    var curList = executeActionGet(r).getList(stringIDToTypeID("targetLayersIndexes")); 
    var curLyrIdx = curList.getReference(0).getIndex() + loopStart;

    layerList.reverse();
    idxList.reverse();
    for(var i=0;i<idxList.length;i++){
        if(idxList[i] == curLyrIdx){
            var cLayerSelc = i}
            }

var dlg = new Window('dialog','Select Layer');
dlg.location = [500,100];
var dList = dlg.add('dropdownlist',undefined,layerList);
dList.selection = cLayerSelc;

dList.onChange = function(){
    selectLayerByIndex (idxList[parseInt(this.selection)],false)
    dlg.close()    
    //selectLayerByIndex (index, add)
    }

dlg.show()

function getLayerSetsData()
{
    //var count = 0;//set counter for multi-dimensional array
    var lyrSets = [];
    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layerSets);
        try
            {var d1 = executeActionGet(ref)}
        catch (err){
            break;
            };

        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyrSet = {};

        lyrSet.name = d1.getString(c2t("Nm  "));   
        lyrSet.type = d1.getInteger(s2t("layerKind"));
        if(lyrSet.type !=13){
            idxList.push(layerSets)
            layerList.push(lyrSet.name);
            }
        layerSets++;
    }; 
    //return lyrSets;
};

function selectLayerByIndex(index, add) {
  add = (add == undefined) ? add = false : add;
  var ref = new ActionReference();
  ref.putIndex(charIDToTypeID("Lyr "), index);
  var desc = new ActionDescriptor();
  desc.putReference(charIDToTypeID("null"), ref);
  desc.putBoolean(charIDToTypeID("MkVs"), false);
  try {
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
  } catch (e) {}
};

 

2 replies

Chuck Uebele
Community Expert
Community Expert
October 23, 2019

It is possible to put on a panel/pallet, but it's a lot more involved. I no longer do that, as it's too much work.

The same would apply to the list for the script folder. You could create actions to run your scripts then they would appear in the action panel.

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
October 22, 2019

I'm not sure what you mean by previous specified folder. Is that the folder where the current active document is located?

Here's a script for listing the layers:

#target photoshop
var doc = activeDocument;
    var layerList = [];
    var idxList = [];
    var layerSets = 0
    try{doc.backgroundLayer}
    catch(e){layerSets=1}
    var loopStart = layerSets;
    var layerListFirst = getLayerSetsData();  
    var r = new ActionReference();
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayersIndexes"));
    r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    var curList = executeActionGet(r).getList(stringIDToTypeID("targetLayersIndexes")); 
    var curLyrIdx = curList.getReference(0).getIndex() + loopStart;

    layerList.reverse();
    idxList.reverse();
    for(var i=0;i<idxList.length;i++){
        if(idxList[i] == curLyrIdx){
            var cLayerSelc = i}
            }

var dlg = new Window('dialog','Select Layer');
dlg.location = [500,100];
var dList = dlg.add('dropdownlist',undefined,layerList);
dList.selection = cLayerSelc;

dList.onChange = function(){
    selectLayerByIndex (idxList[parseInt(this.selection)],false)
    dlg.close()    
    //selectLayerByIndex (index, add)
    }

dlg.show()

function getLayerSetsData()
{
    //var count = 0;//set counter for multi-dimensional array
    var lyrSets = [];
    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layerSets);
        try
            {var d1 = executeActionGet(ref)}
        catch (err){
            break;
            };

        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyrSet = {};

        lyrSet.name = d1.getString(c2t("Nm  "));   
        lyrSet.type = d1.getInteger(s2t("layerKind"));
        if(lyrSet.type !=13){
            idxList.push(layerSets)
            layerList.push(lyrSet.name);
            }
        layerSets++;
    }; 
    //return lyrSets;
};

function selectLayerByIndex(index, add) {
  add = (add == undefined) ? add = false : add;
  var ref = new ActionReference();
  ref.putIndex(charIDToTypeID("Lyr "), index);
  var desc = new ActionDescriptor();
  desc.putReference(charIDToTypeID("null"), ref);
  desc.putBoolean(charIDToTypeID("MkVs"), false);
  try {
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
  } catch (e) {}
};

 

Known Participant
October 23, 2019

Thank you so much! Exactly what I was looking for.

Is it possible to put on a palette?
I apologize, I did not see that the post was published and later created the same.
In the question about folders, I meant any folder on the computer that we initially select with the script.