Skip to main content
Known Participant
March 14, 2024
Answered

Check if layer is selected

  • March 14, 2024
  • 2 replies
  • 928 views

Hello guys i'm currently facing a problem here, i want to check if photoshop is currently selected a group , if yes run the function, if not alert the feedback but i don't know why it not working this is the code i wrote 

 

#target photoshop

// Function to check if a layer is selected
function isLayerSelected() {
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
return executeActionGet(ref).hasKey(stringIDToTypeID("targetLayers"));
}

// Main function
function main() {
if (isLayerSelected()) {
// Call your function here if a layer is selected
loadFunction();
} else {
// Display a warning if no layer is selected
alert("No layer is currently selected.");
}
}

// Function to be called when a layer is selected
function loadFunction() {
app.doAction("Test", "CGIBA");
}
app.activeDocument.suspendHistory("Test", "func2()");
This topic has been closed for replies.
Correct answer Stephen Marsh

Is there a possibility that no layers or groups would be selected? I ask because there is a scripting limitation (bug) that reports the top layer as selected when no layers are selected!

 

alert(activeDocument.activeLayer.name);

 

@jazz-y offered a solution:

 

// by jazz-y
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {
    alert('layers selected');
} else {
    alert('no layers selected');
}

 

Therefore, combining the code from both @jazz-y and @c.pfaffenbichler –

 

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {
    if (activeDocument.activeLayer.typename == "LayerSet") {
        alert("is group");
    } else {
        alert("is layer");
    }
} else {
    alert('no layers selected');
}

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
March 14, 2024

Is there a possibility that no layers or groups would be selected? I ask because there is a scripting limitation (bug) that reports the top layer as selected when no layers are selected!

 

alert(activeDocument.activeLayer.name);

 

@jazz-y offered a solution:

 

// by jazz-y
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {
    alert('layers selected');
} else {
    alert('no layers selected');
}

 

Therefore, combining the code from both @jazz-y and @c.pfaffenbichler –

 

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {
    if (activeDocument.activeLayer.typename == "LayerSet") {
        alert("is group");
    } else {
        alert("is layer");
    }
} else {
    alert('no layers selected');
}

 

Stephen Marsh
Community Expert
Community Expert
March 14, 2024

The final "problem" is that the DOM layerSet is used to denote layer groups, frames and artboards!

 

Which is where AM code comes to the rescue if one needs to distinguish between the 3:

 

c.pfaffenbichler
Community Expert
Community Expert
March 14, 2024

Good catch! 

I use Artboards and Frames so seldom that I am prone to ignoring them. 

c.pfaffenbichler
Community Expert
Community Expert
March 14, 2024

One can even use DOM code to determine if the active layer is a group. 

if (activeDocument.activeLayer.typename == "LayerSet") {alert ("is group")}
else {alert ("is not group")}