Using 'layer via copy' as script

Hello everyone,

I’m currently developing a plugin for Adobe Photoshop that includes a ‘Layer via Copy’ feature. So far, I’ve managed to make several paths and select them using the following script

core.executeAsModal(async () => {
const currentPathItem = app.activeDocument.pathItems[0];
currentPathItem.makeSelection(0.0, true);
});

This part of the script seems to work fine. It selects the first path item in the active document, creating a selection with a 0.0 feather radius and anti-aliasing enabled.

However, I’m encountering difficulties with the next step. In Photoshop, after making a selection, pressing Ctrl+J creates a new layer from the selected area. I’m looking for a way to automate this action using UXP scripting code.

I’ve come across several tips suggesting the use of batchPlay, among other methods, but none have worked for me so far. I’m curious if there’s an alternative approach to achieve the Ctrl+J functionality within my script.

thank you!

What does Alchemist inspector produce when clickingk Ctrl+J?

thank you for your kind answer!!

image

it says like this and when I scripting

let app = require(“photoshop”).app;
let core = require(“photoshop”).core;

async function duplicateSelectionToNewLayer() {
return await require(“photoshop”).action.batchPlay(
[
{
_obj: “copyToLayer”,
isCommand: false,
},
],
{}
);
}

module.exports.duplicateSelectionToNewLayer = duplicateSelectionToNewLayer;

document.getElementById(“batch”).addEventListener(“click”, async function () {
await duplicateSelectionToNewLayer();
});

seems it doesn’t work!

add executeAsModal . . . . . . . .