Skip to main content
Known Participant
April 7, 2020
Question

Script won't work unless a layer is selected

  • April 7, 2020
  • 3 replies
  • 1713 views

I'm trying to get this script to work with no layers selected in the layers palette.

 

The script simply loads the layer mask as a selection of a layer called "LAYER 1".

The Photoshop document contains three layers with the names "LAYER 1", "LAYER 2", and "LAYER 3"

As long as one of the three layers are selected in the layers palette the script loads the layer mask of "LAYER 1" as a selection. If no layers are selected it shows this error:

 

"The command “Set” is not currently available."

 

Is there a different way to write the AM code so the script will work even if no layer is selected in the layers palette?

 

var layer = app.activeDocument.layers.getByName("LAYER 1");

try {
    var selectionReference = new ActionReference();
    var selectionDescriptor = new ActionDescriptor();
    selectionReference.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
    selectionDescriptor.putReference(stringIDToTypeID("null"), selectionReference);
    
    var userMaskReference = new ActionReference();
    userMaskReference.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("ordinal"), stringIDToTypeID("mask"));
    userMaskReference.putIdentifier(stringIDToTypeID("layer"), layer.id);
    selectionDescriptor.putReference(stringIDToTypeID("to"), userMaskReference);
    
    executeAction(stringIDToTypeID("set"), selectionDescriptor, DialogModes.NO);

} catch (error) {
    alert(error);
}

 

 

This topic has been closed for replies.

3 replies

Legend
April 8, 2020
At the end of that topic (by my link) there is code to restore the selection of the active layer (if nothing is selected).
 
This is one of the options.
 
Legend
April 7, 2020
mikebaughAuthor
Known Participant
April 8, 2020

Kukurykus,

 

I eventually got that code snippet to work. However, the script will still fail if layer sets are involved.

 

r-bin,

 

Thank you for the link!

 

I think I can come up with a hacky solution with some of that information.

 

 

mikebaughAuthor
Known Participant
April 8, 2020

A couple of scripts I use only work if a layer is selected in the layers palette. It doesn't matter which layer is selected it just needs a layer that is not a layer set. Apparently the set command to load a mask as a selection only consistently works if a layer is selected.

 

Based on the links Kurkuryus and r-bin suggested I cobbled together the following code to solve the issue.

 

I'm going to try to develop a more elegant and simpler solution, but this works for now.

 

// Layer Selected
function layerSelected() {
    var documentReference = new ActionReference();
    documentReference.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
    documentReference.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    return executeActionGet(documentReference).hasKey(stringIDToTypeID("targetLayers"));
}

// Recursive Function Select A Layer
function selectLayer(layerSet) {
    var layer,
        layerReference,
        selectDescriptor,
        i;

    for (i = 0; i < layerSet.layers.length; i += 1) {
        layer = layerSet.layers[i];
        if (layer.typename !== "LayerSet") {
            if (!layerSelected() || app.activeDocument.activeLayer.typename === "LayerSet") {
                layerReference = new ActionReference();
                layerReference.putName(stringIDToTypeID("layer"), layer.name);
                selectDescriptor = new ActionDescriptor();
                selectDescriptor.putReference(stringIDToTypeID("null"), layerReference);
                executeAction(stringIDToTypeID("select"), selectDescriptor, DialogModes.NO);
                return;
            }
        } else {
            selectLayer(layer);
        }
    }
}

// Main
function main() {
    if (!layerSelected() || app.activeDocument.activeLayer.typename === "LayerSet") {
        selectLayer(app.activeDocument);
    }
}

main();

 

 

 

Kukurykus
Legend
April 7, 2020
mikebaughAuthor
Known Participant
April 7, 2020

Hi Kukurykus,

 

As always thank you for your help!

 

I wasn't able to implement the code in the above link successfully.

 

I'm looking for a way to load a layer's layer mask as a selection by targeting the layer's mask using a layer index or layer name and without having to have any layers selected in the layers palette.

 

BTW, registration is still not working on https://www.ps-scripts.com/ucp.php?mode=register

There is an error on the page "This site key is not enabled for the invisible captcha."

Kukurykus
Legend
April 7, 2020

Didn't you put the code from last post on forum I likned you to, at beginning of your code?

 

Post Scriptum: I let Administrator Tom know you have peoblem with registration on ps-scripts