Skip to main content
Participant
January 13, 2025
Question

Is possible to using js to make a group selected and click it?

  • January 13, 2025
  • 4 replies
  • 238 views

I know 
app.activeDocument.activeLayer = targetGroup;

But it still not make them selected.

is any solution to make it selected(hightlight in photoshop layer management)

4 replies

Stephen Marsh
Community Expert
Community Expert
January 14, 2025

@dfsd_3534 – Try this code (the task would be much simpler and faster without multiple artboards):

 

#target photoshop

// Get the active document
var doc = app.activeDocument;

// Find all the 'design' layer groups in the document
var designGroups = getDesignGroup(doc.layers);

// If there's at least one 'design' group, select the first one
if (designGroups.length > 0) {
    doc.activeLayer = designGroups[0];
} else {
    alert("No 'design' layer group found.");
}

// Function to loop through all layer groups and find the child group named 'design'
function getDesignGroup(layerSet) {
    var groups = new Array();

    for (var i = 0; i < layerSet.length; i++) {
        var layer = layerSet[i];

        // If the layer is a layer group
        if (layer.typename === 'LayerSet') {
            // If the layer group case-sensitive name is 'design'
            if (layer.name === 'design') {
                groups.push(layer);
            } else {
                // Recursively search in subgroups
                var subGroups = getDesignGroup(layer.layers);
                groups = groups.concat(subGroups);
            }
        }
    }
    return groups;
}

 

Stephen Marsh
Community Expert
Community Expert
January 13, 2025
quote
app.activeDocument.activeLayer = targetGroup;

By @dfsd_3534

 

Where/how is the variable for targetGroup defined?

 

Is there only one group in the doc?

 

Does the group have a unique specific name to select by?

 

Does the group have a specific position in the layer stack?

 

Are artboards ever in use?

dfsd_3534Author
Participant
January 14, 2025
quote
quote

 

app.activeDocument.activeLayer = targetGroup;


By @dfsd_3534

 

 

Where/how is the variable for targetGroup defined?


here is my function to get selected grounp which name "design"

function getDesignGroup(doc) {
    var designGroup = null;
    // Ensure the script is focusing on the document before accessing its layers
    app.activeDocument = doc;

    // Log document name and layer sets for debugging
    $.writeln("Active document: " + doc.name);
    $.writeln("Number of layer sets: " + doc.layerSets.length);

    // Loop through all layer sets (groups) in the document
    for (var i = 0; i < doc.layerSets.length; i++) {
        $.writeln("Layer set: " + doc.layerSets[i].name);  // Log each layer set's name
        if (doc.layerSets[i].name === "design") {
            designGroup = doc.layerSets[i];
            break;
        }
    }
    return designGroup;
}

Is there only one group in the doc?


No, but at least one.

Does the group have a unique specific name to select by?


yes

Does the group have a specific position in the layer stack?


the group on the top level.

Are artboards ever in use?


Yes, multi windows in use.

By @Stephen Marsh

 

c.pfaffenbichler
Community Expert
Community Expert
January 13, 2025

The screenshot seems to indicate that the group is selected. 

dfsd_3534Author
Participant
January 13, 2025

Sorry for my typo in title 

Is possible to using js to make a group selected like click it?