activate multi layers name with listbox selected items text
Greetings to everyone and I wish you lasting success
I found this code to select more than one layer and it works well
select_layer("Layer 1");
select_layer("Layer 2", true);
function select_layer(nm, add)
{
try {
var r = new ActionReference();
r.putName(stringIDToTypeID("layer"), nm);
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r);
if (add == true) d.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
}
catch (e) { alert(e); throw(e); }
}But I would like to add it to my code
When more than one item is selected inside the listbox, the layers are activated using the names of the selected items inside the listbox
this is my code
#target photoshop
// Create a new window
var win = new Window("dialog", "Photoshop Script");
//win.bounds = [0, 0, 500, 200];
// Create a GroupListBox to display visible group names
var groupListBox = win.add("listbox", [10, 10, 240, 180], [], {multiselect: false});
groupListBox.onChange = updateChildLayerListBox;
// Create a ChildLayerListBox to display child layer names
var childLayerListBox = win.add("listbox", [250, 10, 490, 180], [], {multiselect: false});
childLayerListBox.onChange = activateChildLayer;
// Create a StaticText to display the count of items in the ChildLayerListBox
var staticText = win.add("statictext", [250, 185, 490, 200], "");
// Get the active document
var doc = app.activeDocument;
// Get all visible group names in the active document and populate the GroupListBox
var visibleGroupNames = getVisibleGroupNames(doc);
populateListBox(groupListBox, visibleGroupNames);
// Function to get all visible group names in the active document
function getVisibleGroupNames(document) {
var groupNames = [];
// Loop through all layers in the document
for (var i = 0; i < document.layers.length; i++) {
var layer = document.layers[i];
// Check if the layer is a group and visible
if (layer.typename === "LayerSet" && layer.visible) {
groupNames.push(layer.name);
}
}
return groupNames;
}
// Function to populate a listbox with items
function populateListBox(listBox, items) {
listBox.removeAll();
for (var i = 0; i < items.length; i++) {
listBox.add("item", items[i]);
}
}
// Function to update the ChildLayerListBox when the selection in the GroupListBox changes
function updateChildLayerListBox() {
var selectedGroupName = groupListBox.selection.text;
// Get the child layers of the selected group
var childLayerNames = getChildLayerNames(doc, selectedGroupName);
// Populate the ChildLayerListBox with the child layer names
populateListBox(childLayerListBox, childLayerNames);
// Update the staticText with the count of items in the ChildLayerListBox
staticText.text = "Count: " + childLayerListBox.items.length;
}
// Function to get the child layer names of a group
function getChildLayerNames(document, groupName) {
var layerNames = [];
// Get the group by name
var group = document.layerSets.getByName(groupName);
// Loop through all layers in the group
for (var i = 0; i < group.layers.length; i++) {
var layer = group.layers[i];
// Check if the layer is not a group (child layer)
if (layer.typename !== "LayerSet") {
layerNames.push(layer.name);
}
}
return layerNames;
}
// Function to activate the child layer when the selection in the ChildLayerListBox changes
function activateChildLayer() {
var selectedLayerName = childLayerListBox.selection.text;
// Get the group by name
var selectedGroupName = groupListBox.selection.text;
var group = doc.layerSets.getByName(selectedGroupName);
// Get the child layer by name
var layer = group.layers.getByName(selectedLayerName);
// Activate the child layer
doc.activeLayer = layer;
}
// Show the window
win.show();
