Skip to main content
Participant
April 10, 2023
Question

“Layer Via Copy” is not currently available. Error From UXP Scripting

  • April 10, 2023
  • 2 replies
  • 1987 views

Photoshop v24.1.0

App Version 24.1.0

UXP Version 6.4.0

 

 

Goal:

I am writing a plugin and from the UI I want to click a button and have that trigger a series of functions to do the following.

 

1) Select a layer : "mainimage"

2) Create a selection on that layer

3) Copy that selection to a newlayer 

4) Rename that new layer to : temp_image

5) Export that temp_image layer with a randomized name

 

Method 01:

1) Create an 'action' of the steps listed above

2) Play that action in response to a button trigger from the UI

 

Method 02: 

1) Use a plugin called 'Alchemist" to record the UI actions and convert them into batch commands

2) Run those batch command in sequence in response to a button trigger from the UI

 

Challenge :

 

The entire operation will run successfully the first time. 

Any attempt via the plugin made after the first one will Fail with the following error :

The command “Layer Via Copy” is not currently available.

Running the actions through the UI will work every single time with no error.

 

What I think is happening:

When i execute the command the first time it somehow locks the "copy via layer" command. Each susbsequent call from the plugin fails.  

 

====================== METHOD 01 ==============

 

document.querySelector("#btnNewInstance").onclick = handleNewInstanceBtn;

 

function handleNewInstanceBtn(){

        createTempImageLayer()
        deselectAllLayers()
}

 

 

async function createTempImageLayer() {

const batchCommands = {
_obj: "play",
_target: [
{
_ref: "action",
_name: "Create MainImage"
},
{
_ref: "actionSet",
_name: "Default Actions"
}
],
_options: {
dialogOptions: "dontDisplay"
}
}
console.log("BatchCommands : createTempImageLayer")

return await require("photoshop").core.executeAsModal(async () => {
await require('photoshop').action.batchPlay([batchCommands], { });
}, { commandName: "Make New Text Layer" });
}

 

async function deselectAllLayers() {

const batchCommands = {
_obj: "set",
_target: [
{
_ref: "channel",
_property: "selection"
}
],
to: {
_enum: "ordinal",
_value: "none"
},
_options: {
dialogOptions: "dontDisplay"
}
}

console.log("BatchCommands : deselectAllLayers")

return await require("photoshop").core.executeAsModal(async () => {
await require('photoshop').action.batchPlay([batchCommands], { });
}, { commandName: "Make New Text Layer" });
}

 

====================== METHOD 02 ==============

 

async function makeSelection() {

const batchCommands = {
_obj: "set",
_target: [
{
_ref: "channel",
_property: "selection"
}
],
to: {
_obj: "rectangle",
top: {
_unit: "distanceUnit",
_value: 410.88
},
left: {
_unit: "distanceUnit",
_value: 0
},
bottom: {
_unit: "distanceUnit",
_value: 1296
},
right: {
_unit: "distanceUnit",
_value: 1031.52
}
},
_options: {
dialogOptions: "dontDisplay"
}
}


const batchCommands2 = {
_obj: "copyToLayer",
_options: {
dialogOptions: "dontDisplay"
}
}

 

console.log("batchCommands : makeSelection")

return await require("photoshop").core.executeAsModal(async () => {
await require('photoshop').action.batchPlay([batchCommands,batchCommands2], { synchronousExecution: true,
modalBehavior: "fail"});
}, { commandName: "Make New Text Layer" });
}

 

 

 


async function copyToNewLayer() {

const batchCommands = {
_obj: "copyToLayer",
_options: {
dialogOptions: "dontDisplay"
}
}
console.log("BatchCommands : copyToLayer")

return await require("photoshop").core.executeAsModal(async () => {
await require('photoshop').action.batchPlay([batchCommands], { });
}, { commandName: "Make New Text Layer" });
}

 

 

 

Any advice would be useful and very well recieved ...!!!

 

Thanks

2 replies

jduncan
Community Expert
Community Expert
May 20, 2025

Hey, recreated your action and then used the " Copy as JavaScript" from the flyout menu and the code works repeadly on the same file and new files just fine for me... You'll need to update the seleciton location but hopefully this works for you. I have also included an alternate solution, I hacked up using a few techniques...

 

 

// code generated from PS actions panel
async function doWork() {
    let commands = [
        // Set Selection
        {
            _obj: "set",
            _target: [
                {
                    _property: "selection",
                    _ref: "channel",
                },
            ],
            to: {
                _obj: "rectangle",
                bottom: {
                    _unit: "distanceUnit",
                    _value: 172.32,
                },
                left: {
                    _unit: "distanceUnit",
                    _value: 67.2,
                },
                right: {
                    _unit: "distanceUnit",
                    _value: 196.32,
                },
                top: {
                    _unit: "distanceUnit",
                    _value: 54.72,
                },
            },
        },
        // Layer Via Copy
        {
            _obj: "copyToLayer",
        },
        // Set current layer
        {
            _obj: "set",
            _target: [
                {
                    _enum: "ordinal",
                    _ref: "layer",
                    _value: "targetEnum",
                },
            ],
            to: {
                _obj: "layer",
                name: "temp_image",
            },
        },
    ];
    return await require("photoshop").action.batchPlay(commands, {});
}

console.log(
    await require("photoshop").core.executeAsModal(doWork, {
        commandName: "Action Commands",
    })
);

 

Alternate solution:

const { app, core, constants } = require("photoshop");
const { batchPlay } = require("photoshop").action;

async function makeSelection() {
    return await core.executeAsModal(async () => {
        app.activeDocument.selection.selectRectangle(
            { top: 50, left: 50, bottom: 100, right: 100 },
            constants.SelectionType.REPLACE
        );
    });
}

async function layerViaCopy() {
    return await core.executeAsModal(async () => {
        let command = [{ _obj: "copyToLayer" }];
        return await batchPlay(command, {});
    });
}

async function renameNewLayer() {
    return await core.executeAsModal(async () => {
        app.activeDocument.activeLayers[0].name = "temp_image";
    });
}

await makeSelection();
await layerViaCopy();
await renameNewLayer();
adrianr60826806
Participating Frequently
May 20, 2025

Hi JDuncan, 

Thank you for attempting to help. But I'm confused. I could not find the flyout menu you refer to. Would I need to recreate the action from scratch and enable this before I recreate it? I also don't know how or where to include the code you have kindly provided. A few notes:

1.) I have had some success running this automated action on single .CR3 files but no success in running an automation of a batch of images.

2.) I did update photoshop which seems to have somehow obscured / changed a setting making it now impossible to even run an action on a single file. It gets stuck on every stage in the automation. 

 

I tried to include my automation but they would not let me upload the file type unfortunately.  

 

Many thanks,

 

Adrian

 

 

adrianr60826806
Participating Frequently
May 20, 2025

Could it be due to an embedded color profile mismatch, which I am seeing when I first open a .CR3 image in photoshop? Not sure how to resolve that either! 

adrianr60826806
Participating Frequently
May 20, 2025

I'm having the same issue.