I want to export a file in PNG format, do you have a sample code?

I would like to save an image file with a UXP script using document.saveAs.png in the API linked below. Do you have a sample code?
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/

Photoshop 2023 (24.6.0) で動くサンプルができました。
なぜかPhotoshop 2022 (23.5.5) では,saveAsで「Error: NAPI API failure: String expected」と出て動きません。

I was able to get a working sample in Photoshop 2023 (24.6.0).
For unknown reason, Photoshop 2022 (23.5.5) does not work with “Error: NAPI API failure: String expected” in saveAs.

/**
  * @file UXP script (psjs) to save active document to the location specified in the dialog. Works in Photoshop 2023 (24.6.0)
  * @version 1.0.0
  * @author sttk3.com
  * @copyright © 2023 sttk3.com
*/

const { app } = require('photoshop') ;
const { localFileSystem } = require('uxp').storage ;

if(app.documents.length <= 0) {return ;}
const doc = app.activeDocument ;
const dstEntry = await localFileSystem.getFileForSaving(`${doc.name.replace(/\.[a-z0-9]{2,4}$/i, '')}.png`) ;
if(dstEntry == null) {return ;}

try {
  // entry, PNGSaveOptions, asCopy
  await doc.saveAs.png(dstEntry, {compression: 6}, true) ;

  await app.showAlert('Export succeeded.') ;
} catch(e) {
  await app.showAlert(e) ;
}
1 Like

コードサンプルを投稿いただき誠にありがとうございます。早速試してみます。
Thank you very much for posting the code sample. I try immediately.

Photoshop2023(24.6.0)で無事にコードが実行できました。ありがとうございました!私の問題点は、try catchのエラー表示をしていなかった点で、エラーの発見がわからずでした。あとexecuteAsModalの中で実行することでした。

The code was successfully executed in Photoshop2023 (24.6.0). thank you very much! My problem was that the try catch error was not displayed, so I didn’t know how to find the error. And it was to execute in executeAsModal.

どういたしまして。うまくいってよかったです。

You are welcome. Glad it worked out.

1 Like