Copy link to clipboard
Copied
Greetings everybody!!!
This code below is not my full code so don't try it in Photoshop. I just show to you the most important parts.
Well, I have a dropdownlist control, which lists some layer types and their numeric indexes. Then I make a layerType variable (with layer types numeric indexes) which is useful for getLayersData function, so to find all layers with specific type.
I also have a second dropdownlist, which lists all found layer names and a layerName variable which is also useful for getLayersData function, so to find all layers with specific name.
My problem is, that when I run the script, getLayersData function gets the layerType variable only (no problem) and makes an array with the layers of that type. But if I change the layerType from dropdownlist1, the list of dropdownlist2 remains as it is, with the layers of previous type!!!
So, how can I make my dropdownlist2 (layers names) reload, refresh (whatever it is called), when I change the layer type at dropdownlist1?
Thank you in advance!!!
function main()
{
var dropdownlist1_array = ["Any Layer","Pixel Layer","Adjustment","Text Layer","Vector Layer","Smart Object","Video Layer","Group or Artboard","3D Layer","Gradient","Pattern","Solid Color","Background Layer","Hidden Section Bounder"];
var dropdownlist1 = group2.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = 4;
dropdownlist1.alignment = ["left","center"];
var layerType = dropdownlist1.selection.index;
dropdownlist1.onChange = function(){layerType = this.selection.index; statictext2.text = layerType; app.refresh();};
var statictext2 = group2.add("statictext");
statictext2.justify = "left";
statictext2.text = layerType;
statictext2.orientation = "column";
statictext2.spacing = 0;
var layerNames = [];
var layersData = getLayersData();
for (var i = 0; i < layersData.length; i++){layerNames.push(layersData.name);}
layerNames = UniqueSortedList(layerNames);
var dropdownlist2 = group3.add("dropdownlist", undefined, layerNames);
dropdownlist2.selection = 0;
var layerName = dropdownlist2.selection.index;
dropdownlist2.onChange = function(){layerName = this.selection; edittext1.text = layerName; app.refresh();};
dialog.show();
function getLayersData()
{
var lyrs = [];
var layers = 1;
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try{var desc = executeActionGet(ref);}catch (err){break;};
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
if (lyr.type == layerType && lyr.name.match(layerName))
{
lyrs.push(lyr);
};
layers++;
};
return lyrs;
};
function UniqueSortedList(ArrayName)
{
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++)
{
unduped[ArrayName] = ArrayName;
}
var uniques = new Array;
for (var k in unduped)
{
uniques.push(unduped
); }
return uniques.sort();
};
};
Just altered that function so it returns all data, then it can be sorted later. I.E.
...
var w = new Window("dialog","test",undefined,{closeButton: true});
var group4 = w.add("group");
var dropdownlist1_array = ["Any Layer [0]","Pixel Layer [1]","Adjustment [2]","Text Layer [3]","Vector Layer [4]","Smart Object [5]","Video Layer [6]","Group or Artboard [7]","3D Layer [8]","Gradient [9]","Pattern [10]","Solid Color [11]","Background Layer [12]"]; // "Hidden Section Bounder [13]"
var
Copy link to clipboard
Copied
Here is an example...
var array1 = ["Joe","Fred","Brian"];
var array2 = ["Yellow","Green","Blue"];
var array3 = ["Earth","Mars","Venus"];
var w = new Window("dialog","test",undefined,{closeButton: true});
w.group1 = w.add("group");
var dropdownlist1_array = ["Any Layer","Pixel Layer","Adjustment"];
w.group1.dl1 = w.group1.add("dropdownlist",undefined,dropdownlist1_array);
w.group1.dl1.selection=0;
w.group2 = w.add("group");
w.group2.dl1 = w.group2.add("dropdownlist",undefined,array1);
w.group2.dl1.selection=0;
w.group1.dl1.onChange=function(){
w.group2.dl1.removeAll();
switch(w.group1.dl1.selection.index){
case 0: for(var a = 0; a< array1.length;a++) {w.group2.dl1.add("item", array1);}
break;
case 1: for(var a = 0; a< array2.length;a++) {w.group2.dl1.add("item", array2);}
break;
case 2: for(var a =0; a< array3.length;a++) {w.group2.dl1.add("item", array3);}
break;
}
w.group2.dl1.selection=0;
}
w.show();
Copy link to clipboard
Copied
Well based on you example too, I managed to make it work, I just had to call the getLayersData function and do all the stuff inside dropdownlist1.onChange, but I have an issue. The second dropdownlist control filled up with unique layer names as it should, but again, I have the same problem as in this question!!! Instead of a list with names I get one line with all names!!! Here is my code as it looks now...
var dropdownlist1_array = ["Any Layer [0]","Pixel Layer [1]","Adjustment [2]","Text Layer [3]","Vector Layer [4]","Smart Object [5]","Video Layer [6]","Group or Artboard [7]","3D Layer [8]","Gradient [9]","Pattern [10]","Solid Color [11]","Background Layer [12]"]; // "Hidden Section Bounder [13]"
var dropdownlist1 = group4.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = -1;
dropdownlist1.preferredSize.height = 20;
dropdownlist1.onChange = function(){onChangeDropdownlist1();};
var dropdownlist2 = group4.add("dropdownlist", undefined, undefined);
dropdownlist2.selection = -1;
dropdownlist2.preferredSize.height = 20;
function onChangeDropdownlist1()
{
dropdownlist2.removeAll();
layerType = dropdownlist1.selection.index;
var layerNames = [];
var layersData = getLayersData();
for (var i = 0; i < layersData.length; i++){layerNames.push(layersData.name);}
layerNames = UniqueSortedList(layerNames);
dropdownlist2.add("item", layerNames);
dropdownlist2.selection = 0;
};
function getLayersData()
{
var lyrs = [];
var layers = 1;
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try{var desc = executeActionGet(ref);}catch (err){break;};
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
if (lyr.type == layerType && lyr.name.match(layerName))
{
lyrs.push(lyr);
};
layers++;
};
return lyrs;
};
function UniqueSortedList(ArrayName)
{
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++)
{
unduped[ArrayName] = ArrayName;
}
var uniques = new Array;for (var k in unduped)
{
uniques.push(unduped
); }
return uniques.sort();
};
Copy link to clipboard
Copied
Please try this...
var w = new Window("dialog","test",undefined,{closeButton: true});
var group4 = w.add("group");
var dropdownlist1_array = ["Any Layer [0]","Pixel Layer [1]","Adjustment [2]","Text Layer [3]","Vector Layer [4]","Smart Object [5]","Video Layer [6]","Group or Artboard [7]","3D Layer [8]","Gradient [9]","Pattern [10]","Solid Color [11]","Background Layer [12]"]; // "Hidden Section Bounder [13]"
var dropdownlist1 = group4.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = 0;
dropdownlist1.preferredSize.height = 20;
dropdownlist1.onChange = function(){onChangeDropdownlist1();};
var dropdownlist2 = group4.add("dropdownlist", undefined, undefined);
dropdownlist2.selection = -1;
dropdownlist2.preferredSize.height = 20;
w.show();
function onChangeDropdownlist1()
{
dropdownlist2.removeAll();
var layerNames = getLayersData(dropdownlist1.selection.index);
layerNames = UniqueSortedList(layerNames);
for(var z in layerNames) {dropdownlist2.add("item", layerNames
); } dropdownlist2.selection = 0;
};
function getLayersData(layerType)
{
var lyrs = [];
var layers = 1;
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try{var desc = executeActionGet(ref);}catch (err){break;};
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
if (Number(lyr.type) == layerType)
{
lyrs.push(lyr.name);
};
layers++;
};
return lyrs;
};
function UniqueSortedList(ArrayName)
{
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++)
{
unduped[ArrayName] = ArrayName;
}
var uniques = new Array;for (var k in unduped)
{
uniques.push(unduped
); }
return uniques.sort();
};
Copy link to clipboard
Copied
It works that too, but I am trying to find a way to make all changes in onChangeDropdownlist1 function, because I need getLayersData() function as it is!!!
Copy link to clipboard
Copied
Just altered that function so it returns all data, then it can be sorted later. I.E.
var w = new Window("dialog","test",undefined,{closeButton: true});
var group4 = w.add("group");
var dropdownlist1_array = ["Any Layer [0]","Pixel Layer [1]","Adjustment [2]","Text Layer [3]","Vector Layer [4]","Smart Object [5]","Video Layer [6]","Group or Artboard [7]","3D Layer [8]","Gradient [9]","Pattern [10]","Solid Color [11]","Background Layer [12]"]; // "Hidden Section Bounder [13]"
var dropdownlist1 = group4.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = 0;
dropdownlist1.preferredSize.height = 20;
dropdownlist1.onChange = function(){onChangeDropdownlist1();};
var dropdownlist2 = group4.add("dropdownlist", undefined, undefined);
dropdownlist2.selection = -1;
dropdownlist2.preferredSize.height = 20;
w.show();
function onChangeDropdownlist1()
{
dropdownlist2.removeAll();
var layerData = getLayersData(dropdownlist1.selection.index);
var layerNames=[];
for(var a in layerData){if(layerData.type == dropdownlist1.selection.index) layerNames.push(layerData.name);}
layerNames = UniqueSortedList(layerNames);
for(var z in layerNames) {dropdownlist2.add("item", layerNames
); } dropdownlist2.selection = 0;
};
function getLayersData()
{
var lyrs = [];
var layers = 1;
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try{var desc = executeActionGet(ref);}catch (err){break;};
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
lyrs.push(lyr);
layers++;
};
return lyrs;
};
function UniqueSortedList(ArrayName)
{
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++)
{
unduped[ArrayName] = ArrayName;
}
var uniques = new Array;for (var k in unduped)
{
uniques.push(unduped
); }
return uniques.sort();
};
Copy link to clipboard
Copied
You are super, thank you very much!!!
Copy link to clipboard
Copied
I had to miss somehow removeAll() method, so I always thought only way to refresh elements in a list is reopen the dialog.