Export JPEG and get file size in UXP

Hi, I need to create a function in UXP which exports a JPEG and returns the filesize.

In ExtendScript, all I had to do was run this function:

function SaveJPEG(saveFilepath, quality) {
    var exportOptions = new ExportOptionsSaveForWeb()
    exportOptions.format = SaveDocumentType.JPEG
    exportOptions.includeProfile = false
    exportOptions.interlaced = false
    exportOptions.optimized = true
    exportOptions.quality = quality //0 - 100
    var saveFile = File(saveFilepath)
    app.activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, exportOptions)
    return saveFile.length

}

I cannot see an obvious API in UXP for this, so I’m assuming it now has to be performed via BatchPlay.

I captured this code using Alchemist, but not sure if it is the correct command for exporting a JPEG. Plus none of the commands captured in Alchemist contain any information about the JPEG quality.

const batchPlay = require("photoshop").action.batchPlay;

const result = await batchPlay(
[
   {
      "_obj": "modalHTMLPending",
      "kcanDispatchWhileModal": true,
      "isPending": false,
      "extension": "com.adobe.WEBPA.crema.saveforwebdocument",
      "_isCommand": false,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});

Any help would be greatly appreciated.

When I activate the listener and do a “File > Save As > Jpeg”, I get the following descriptor:

{
   "_obj": "save",
   "as": {
      "_obj": "JPEG",
      "extendedQuality": 9,
      "matteColor": {
         "_enum": "matteColor",
         "_value": "none"
      }
   },
   "in": {
      "_path": *token here*,
      "_kind": "local"
   },
   "documentID": 310,
   "lowerCase": true,
   "saveStage": {
      "_enum": "saveStageType",
      "_value": "saveBegin"
   },
   "_isCommand": false
}

I haven’t tested to execute it though. It might be not executable that way, as it includes _isCommand: false.

Regarding the file size: There’s a size property on the EntryMetadata

Thank you, I can confirm that all works! I generated the same descriptor using File > Save a Copy > JPEG.

Hello, could you please tell is there any solution for exporting with parameters available now?
In my case Save As command is not an alternative to Export as I need to resize (make smaller) the PSD file only on export time. PSD has smart objects with Gaussian blur smart filters applied and in case of making PSD image size smaller, the blur amount does not change automatically making invalid output when saving via Save As.

Unfortunatly Alchemist doesn’t record all the parameters applied or I’ve recorded incorrectly?

const batchPlay = require("photoshop").action.batchPlay;

const result = await batchPlay(
[
   {
      "_obj": "modalHTMLPending",
      "kcanDispatchWhileModal": true,
      "isPending": false,
      "extension": "com.adobe.WEBPA.crema.saveforwebdocument",
      "_isCommand": false,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});

@kerrishotts @DavideBarranca @Jarda please advise.

Regards

you can see that _isCommand is false therefore this event is for your information only and cannot be played.

I think that the new dialog for export is not yet recordable. Use a different ways to save documents if you need to.

1 Like

Hello, the link below might help for saving a file in UXP.
Anything to do with files read/write is needed to be approached differently than in CEP

https://forums.creativeclouddeveloper.com/t/saving-a-jpg-to-a-selected-folder/2838

This link provide essential info about files in UXP:
https://www.adobe.io/photoshop/uxp/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/File/

Thanks for the quick response. As a workaround I think I’ll have to save as jpeg, then reopen, resize and save for now until this basic functionality will be available in UXP API.

I’m confused now, what exactly are you after?

Actually I am able to save the document with the new workflow (entry, tokens). I just need to export PSD to jpeg setting the width and height. But unfortunately Export feature is not available at all. Do you have suggestion on this?

Typically what I do is that I duplicate the current document PSD, I resize it, sharpen it and what not, and then export that PSD to jpeg file.