Skip to main content
Andre Nguyen
Inspiring
July 26, 2024
Question

UXP Script to select the layer directly above/under the currently selected layer including hidden?

  • July 26, 2024
  • 2 replies
  • 989 views

I use this code to select the above/under the currently selected layer, but this script skips the hidden layer.

How do i modify the script to include the hidden one?

async function selectLayer(executionControl, direction) {
        await batchPlay(
            [
                {
                    _obj: "select",
                    _target: [
                        {
                            _ref: "layer",
                            _enum: "ordinal",
                            _value: direction === Directions.up ? "forwardEnum" : "backwardEnum",
                        },
                    ],
                    makeVisible: false,
                    _options: {
                        dialogOptions: "dontDisplay",
                    },
                },
            ],
            {},
        );
}

 

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
July 26, 2024

I don't know if this is possible with UXP DOM code, however, with UXP batchPlay, it should, in theory, be the same as when using an action:

 

 

Perhaps you could tidy this up (I don't know UXP):

 

// Select Forward Invisible Layer.psjs

async function selectForwardInvisibleLayer() {
    let result;
    let psAction = require("photoshop").action;

    let command = [
        // Show current layer
        { "_obj": "show", "null": [{ "_enum": "ordinal", "_ref": "layer", "_value": "targetEnum" }], "toggleOptionsPalette": true },
        // Select forward layer
        { "_obj": "select", "_target": [{ "_enum": "ordinal", "_ref": "layer", "_value": "forwardEnum" }], "makeVisible": false },
        // Show current layer
        { "_obj": "show", "null": [{ "_enum": "ordinal", "_ref": "layer", "_value": "targetEnum" }], "toggleOptionsPalette": true }
    ];
    result = await psAction.batchPlay(command, {});
}

async function runModalFunction() {
    await require("photoshop").core.executeAsModal(selectForwardInvisibleLayer, { "commandName": "Action Commands" });
}

await runModalFunction();

 

 

// Select Backward Invisible Layer.psjs

async function selectBackwardInvisibleLayer() {
    let result;
    let psAction = require("photoshop").action;

    let command = [
        // Show current layer
        { "_obj": "show", "null": [{ "_enum": "ordinal", "_ref": "layer", "_value": "targetEnum" }], "toggleOptionsPalette": true },
        // Select backward layer
        { "_obj": "select", "_target": [{ "_enum": "ordinal", "_ref": "layer", "_value": "backwardEnum" }], "makeVisible": false },
        // Show current layer
        { "_obj": "show", "null": [{ "_enum": "ordinal", "_ref": "layer", "_value": "targetEnum" }], "toggleOptionsPalette": true }
    ];
    result = await psAction.batchPlay(command, {});
}

async function runModalFunction() {
    await require("photoshop").core.executeAsModal(selectBackwardInvisibleLayer, { "commandName": "Action Commands" });
}

await runModalFunction();

 

I'm sure that there are better ways!

Andre Nguyen
Inspiring
July 29, 2024

Thank you for the response, but unfortunately, this script doesn't work properly if I have group or nested layers.

Stephen Marsh
Community Expert
Community Expert
July 29, 2024

I tested with root/top-level artLayers and layerSets but as it's based on the standard keyboard shortcut for traversing layers it has limitations.

 

Please post a screenshot of the layers panel or attach a sample layered PSD.

 

Sadly UXP hasn't gained much traction in the scripting community outside of plugin development.

 

Good luck!