Script won't work unless a layer is selected
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);
}
