Photoshop UXP how to save document without dialog(file picker)

English, UXP

since Photoshop has switched from CEP to UXP, many decelopers have been confused.
CEP and UXP both of them are based on JavaScript and panel UI is based on HTML ,CSS(basically web technology)
but also they have some differences as well.
this time, a I explain how to save active Document on Photoshop without dialog.

UXP never accepts string type of file path

if have developed ExtendScript, you may know saving document methods require file path as a string type.
just passing file path as a param, it saves document as you like.
so experienced developer believed UXP has same system like it but…

this is batchPlay which saves document as a tiff image. and you might believe file path variable has string type value. but in this case, it throws Error.

Error: invalid file token used

blimey, what is causing the error? what mean file token?
as I said UXP never accept file path as a string type. you need to convert it into file token.

What is file token?

initially UXP didn’t allow access to local directory without dialog for security reason.
but after 2023 or Photoshop 24.2, it allows to access to local file.
this is why you have to modify manifest first.

manifest is basic declaration for UXP. like panel size or requirement version or etc…
this time I want to save document in local directory so I need to allow access and I declared fullAccess on localFileSystem property. other than that, manifest version has to be 5, and min Photoshop version is better to more than 24.2 I suppose.

Using file picker

before talking about save directly, let talk about file dialog. here’s the example below.

this code saves active document where you chose through dialog.
it seems like almost same with ExtendScript but getFileForSaving method doesn’t return string type of path.
it returns something Symbol object. then saveAs.jpg method saves document.
if you pass string type, it throws error. there’s way to deal with string type.

dialog returns something unique object
getFileForSaving method returns something unique object

Convert string into unique object

as I said initially UXP didn’t have to access local directory, but since Photoshop updated 24.2, it supports methods which convert string into unique object. official document lists methods but I show some examples.

Note UXP requires “file://” protocol on older 7.0 UXP version. since updated 7.0, it doesn’t require protocol anymore. of course newer than 7.0 version can accept protocol as well. see the document

desktopPath variable is string type value. and Photoshop UXP supports fileSystem like Node.js so it has fs and <path(tehnically path object not module, window object on UXP system) and os modules see the documen but be careful they are slightly different from Node.js focus details.
anyway getEntryWithUrl method converts string into Entry object which is adapted to UXP fileSystem.

What is Entry Object?

Entry object is unique object UXP has its own system. see more details the document.for now remind UXP local system only accepts Entry object not string type.
and here’s an example which saves all of open documents on Photoshop.

but getEntryWithUrl only can convert file path already exists. if you want to create new file, you have to use createEntryWithUrl method.
in case of batchPlay, convert like this.

Note only batchPlay needs createSessionToken.
some other methods, like saveAs method doesn’t require createSessionToken. UXP local system so complicated this isn’t familiar with any other web system.

if you want to see more specific code, here’s example of plugin which has save functions.
exportImagesTest

Beer 寄付してサイトを応援する。