Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script won't work unless a layer is selected

Explorer ,
Apr 06, 2020 Apr 06, 2020

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

 

 

TOPICS
Actions and scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
Apr 07, 2020 Apr 07, 2020
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 07, 2020 Apr 07, 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."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2020 Apr 07, 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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Apr 07, 2020 Apr 07, 2020
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 07, 2020 Apr 07, 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.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 07, 2020 Apr 07, 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();

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2020 Apr 08, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2020 Apr 08, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Apr 08, 2020 Apr 08, 2020
LATEST
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.
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines