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

 

Known Participant
March 14, 2024

Thank you i use the code and able to tweak it a bit for my personal use, i learn so much since known this platfrom thank a lot for supporting us marsh

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") {
function func1 () {
app.doAction("Test", "CGIBA");
}
app.activeDocument.suspendHistory("Test", "func1()");
} else {
alert("is layer");
}
} else {
alert('no layers selected');

}
Stephen Marsh
Community Expert
Community Expert
March 14, 2024

@GiangHg999 – You're welcome, that is the great thing about the ExtendScript community in these forums – unfortunately, it's too early to see how we will transition to UXP in the future.

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")}