UXP and Alchemist: my code not working - starting out

Hi folks, i’m really new to coding so I’m following some tutorials.
I downloaded the demo-sample panel, the one which reads the name of the layers in Photoshop and it’s working.

As soon as I try to “listen” what I do in Photoshop, even the simplest thing and copy the code from Alchemist to the “index.js”, the panel is not working anymore.

This is the code I have in index.js:

async function showLayerNames() {
   const batchPlay = require("photoshop").action.batchPlay;

   const result = await batchPlay(
   [
      {
         _obj: "duplicate",
         _target: [
            {
               _ref: "layer",
               _enum: "ordinal",
               _value: "targetEnum"
            }
         ],
         version: 5,
         _options: {
            dialogOptions: "dontDisplay"
         }
      }
   ],{
      synchronousExecution: false,
      
   });
   
}

document.getElementById("btnPopulate").addEventListener("click", showLayerNames);

Anyone has a clue why it’s not working in Ps (last version)?
Thanks!

  1. Please change topic name to match the topic itself - it’s not Alchemist, that’s not working
  2. Please use post formatting tools to format your code (or use backticks ``` around the code block)
  3. I believe you would need executeAsModal() for layer duplication

Hi there, thanks for you answer and the tips you gave me.
The problem I’m facing is that any of the actions I do in order to test this process is not working. Duplicating level, applying an Adjustment, etc.

As you mentioned, I also was pointed to the direction of of the “Modal” but I have no idea how to implement it. I’v tried everything came to my mind but nothing.

Is there any guide for beginners? I’m not able to follow this article:https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/executeasmodal/

Any chance you could show me how to implement it into my script please?

Thanks

In your case the simplest approach I think would be something like:

document.getElementById("btnPopulate").addEventListener(
  "click",
  executeAsModal(showLayerNames, {"commandName": "My Script Command"})
);

Also always keep Dev Tools console open. I believe you would’ve seen the error about modal execution right away

See this way it works, here it worked.

document.getElementById('btnPopulate').addEventListener('click', async () => {
    await require("photoshop").core.executeAsModal(async () => {
        showLayerNames();
    })
});


async function  showLayerNames(){
     await require("photoshop").app.batchPlay([
        {
        _obj: "duplicate",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            version: 5,
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
     ],{}) 
}

Thanks folks, I’ll try that!