Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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."
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
Actually I don't see where you used my code 🙂 r-bin is good as well, but mine can be used too instead of his in your layerSelect function if you want to stay with more complex solution you found yourself instead of that I proposed in earlier post.
Copy link to clipboard
Copied
Then use ActionManager to select 'LAYER 1' instead of DOM.
The following code use at beginning of your original script instead of code of me you used before:
sTT = stringIDToTypeID, dsc = new ActionDescriptor();
(ref = new ActionReference()).putName(sTT('layer'), 'LAYER 1')
dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)
Then in your code change layer.id to activeDocument.activeLayer.id
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now