How to export photoshop document as PDF/PNG/SVG/JPG programmatically using UXP?

Currently, I am able to export photoshop document as psd programmatically using UXP. But, I am not able to export photoshop document as PDF/JPG/PNG/SVG programmatically using UXP as it is supported in XD(createRendition). I want to implement similar functionality in photoshop UXP. So, is there any API to implement this functionality?

It is possible, but it’s not a pretty solution.
Based on some old AM code for the QuickExport feature, you can assemble a Descriptor that works with BatchPlay:

export async function quickExport(destFolder: string, layerOnly = false) {
  try {    
    photoshop.action.batchPlay([
      {
        _obj: layerOnly ? "exportSelectionAsFileTypePressed" : "exportDocumentAsFileTypePressed",
        _target: { _ref: "layer", _enum: "ordinal", _value: "targetEnum" },
        fileType: "png",
        quality: 32,
        metadata: 0,
        destFolder,
        sRGB: true,
        openWindow: false,
        _options: { dialogOptions: "dontDisplay" },
      },
    ], { synchronousExecution: true })
  } catch(e) {console.error(e)}
}

I haven’t used the QuickExport feature that much, so I’m not familiar with all the available settings and filetypes, but that should be loggable. Something to consider is that you can only specify a destination folder, not a filename. The filename will automatically match your document or layer name.

Alright, so what to do with that function now?
UXP has a much stricter file-access policy… You only get write-permission in very limited places, every other places are handled via tokens. Just one problem: The QuickExport doesn’t seem to accept a token, instead it only works with paths in form of strings. Here’s what happens when u feed it a valid token (for the dataFolder, so there should be writing permissions):

So as mentioned, just using the path to the dataFolder works. (You can get it via (await fs.getDataFolder()).nativePath, check out the file API Docs)

There’s quite some limitations as you can see. You probably want the user to specify where the file should be saved etc.

Even if we’d assume QuickExport would let you specify a target file for saving, that wouldn’t help alot since (at least for me), the getFileForSaving() function is broken. The intention of that function is to open a file-dialog where the user can select a file, which you can then use later in your code. When leaving out the options parameter it gives the following error:

image

Taking the example from the docs where the types are specified in the options object, it gives the following error:

image

I’m pretty sure my code is correct, so it seems like getFileForSaving() is buggy (at least on windows?).

Maybe @kerrishotts can help here or @ someone from the team to look into this.


TLDR: You can save PNGs (and maybe other fileformats) to the DataFolder without specifying the filename.

1 Like

What is the maximum value of image quality?

1 Like