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.