Trying to get the 'jpeg' option for 'save' under UXP

I have the following function

async function flattenImageIfOneLayer(startLayers) {
  console.log("start layers = ", startLayers);
  console.log("here1");
  if (startLayers === 1) {
    await require('photoshop').action.batchPlay([
      { "_obj": "flattenImage" }
    ], { "syncUI": true });
    console.log("here2");
  }
}

The output I get on the console is “start layers = 1”, “here1” but not “here2”.

It looks as if the ‘flattenImage’ doesn’t run. If I try to “save as” the subsequent image I get offered the “-.psd” save option whereas if I then perform a manual “flatten Image” and try the “save as” I get the option to save as ”-.jpg”.

I am not trying to debug the above code as such, I am trying to get the option to save as "-.jpg”

1 Like

Is your startLayers a string by any chance?

Thanks for the suggestion but no.

Also I have tried parseint(startLayers) and using ‘==’ rather than ‘===’, but no joy.

I understand that Adjustment Layers haven’t made it into the DOM yet, I am wondering whether other functionality is remiss.

The code below seems to work.

Here I have utilised code generated from the ‘Copy as Javascript’ option for an Action which performs ‘flatten image’. (Where previously I had taken advice from various AI tools. It is still unclear why the previous code didn’t work.)


             if (startLayers === 1)
            {
             await flattenImage();
            }


async function flattenImage() {
  try {
      await photoshop.core.executeAsModal(async () => {
          async function flatten() {
              let commands = [
                  // Flatten Image
                  {
                      "_obj": "flattenImage"
                  }
              ];
              return await require("photoshop").action.batchPlay(commands, {});
          }
              await require("photoshop").core.executeAsModal(flatten, {"commandName": "Action Commands"})
      },);
  } catch (error) {
      console.error("Error applying flattenImage:", error);
  }
}