• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Simple code modification

Engaged ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

Greetings to all
I have this code and it creates a list of all the layers and it works very well

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) {}
};

 

But I want a little modification in this code

I want to modify this code twice

Once, it only detects and lists groups and excludes other layers

- again the other way around where it detects other layers and excludes groups

thank you all

TOPICS
Actions and scripting , SDK

Views

104

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 06, 2022 Nov 06, 2022
quote

Once, it only detects and lists groups and excludes other layers

By @Mohamed Hameed

 

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"), stringIDT
...

Likes

Translate

Translate
Community Expert ,
Nov 06, 2022 Nov 06, 2022

Copy link to clipboard

Copied

It's probably more complex than you think and not at all "simple" since you now need to actually check the relations of layers to each other and not just dump a list. The script you provided doesn't do any of that. I've never been much into PS scripting, so I can't advise. Just pointing out that this will require more than just changing one or two lines...

 

Mylenium

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2022 Nov 06, 2022

Copy link to clipboard

Copied

quote

Once, it only detects and lists groups and excludes other layers

By @Mohamed Hameed

 

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 == 7){
            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) {}
};
quote

again the other way around where it detects other layers and excludes groups

By @Mohamed Hameed

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 != 7 && 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) {}
};

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 06, 2022 Nov 06, 2022

Copy link to clipboard

Copied

@jazz-y 

Very very nice work
Thank you very much

If it doesn't bother you

This code works on the new versions of Photoshop, but most of my use is in the Photoshop version
cs5
What is the line that makes me use this code for photoshop cs5 version

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

LATEST

I do not have a CS5 version and I recommend that you adapt the code yourself (you may have to abandon some of the properties received through the ActionManager in favor of DOM objects). 

There was definitely no "targetLayersIndexes" property in CS5, it appeared recently and, it seems, there was no "layerKind" property (added in CS6). Try this:

only layers:

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("targetLayers"));
    r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    var curList = executeActionGet(r).getList(stringIDToTypeID("targetLayers")); 
    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 t2s = function (s) {return app.typeIDToStringID(s);};
        var lyrSet = {};

        lyrSet.name = d1.getString(c2t("Nm  "));   
        lyrSet.type = t2s(d1.getEnumerationValue(s2t("layerSection")));
       
        if(lyrSet.type == 'layerSectionContent'){
            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) {}
};

only groups:

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("targetLayers"));
    r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    var curList = executeActionGet(r).getList(stringIDToTypeID("targetLayers")); 
    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 t2s = function (s) {return app.typeIDToStringID(s);};
        var lyrSet = {};

        lyrSet.name = d1.getString(c2t("Nm  "));   
        lyrSet.type = t2s(d1.getEnumerationValue(s2t("layerSection")));
       
        if(lyrSet.type != 'layerSectionContent' && lyrSet.type !='layerSectionEnd'){
            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) {}
};

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines