Replace content function does not work with batchPlay

Hi! I honestly don’t understand why some basic and necessary functions don’t work when using batchPlay, like replacing content or inserting an image to the document.
Is there any other way to make this work? Thanks
html:

<sp-action-button size="s"  onClick='runScript_04();' class="btnsM"> Desfazer</sp-action-button>

js:

async  function runScript_04() { 
   await action.batchPlay([{"_obj":"placedLayerReplaceContents","layerID":9,"null":{"_kind":"local","_path":"E:\\• Trabalhos\\Photoshop\\Helena\\Processados\\[Amostra] Caneca Toda Branca_01.png"}}], { });
}

Reality:

_path needs to be a token and not the actual path. Check this example for createSessionToken(entry). And you won’t be able to access just any file on the system just like that, without a file picker. And most likely you will have to put your runScript_04() into executeAsModal()

1 Like

Hi @, thanks again for pointing me in the right direction!
I tested this mode when “open”, “place embedded” and “replace a smartObject”, it worked perfectly opening the dialog window.
js

async function runScript_04(){
    const fs = require('uxp').storage.localFileSystem;
    let entry = await fs.getFileForOpening();
    let token = fs.createSessionToken(entry);
    await action.batchPlay([{"_obj":"placedLayerReplaceContents","layerID":3,"null":{"_kind":"local","_path":token}}], { });
}

But I need to add the image of a path directly without the dialog window. that I couldn’t. It would be possible?
“E:\•Library\PG#101.png”

Without file picker you can access only 3 folders:

  • getTemporaryFolder()
  • getDataFolder()
  • getPluginFolder()

All methods described on same docs page.

For file picker you can define the initial location (also see docs) where it will open. Also, when you get the file once, you can generate persistent token and access that file any time later without file picker. Or you can get persistent token to a folder by using getFolder() and then access all files in it recursively without a picker

Without file picker you can access only 3 folders:

getTemporaryFolder()
getDataFolder()
getPluginFolder()

With the new FileSystem API it’s possible to read/write without file picker: https://developer.adobe.com/photoshop/uxp/2022/uxp/reference-js/Modules/FileSystem/

It requires manifest version 5 and PS 24.0.0.

In the manifest also declare:

"requiredPermissions": {
  "localFileSystem": "fullAccess"
}
4 Likes

We don’t have a lot of informations about it yet. So for example readFile returns an ArrayBuffer. Not sure how we can get a file object that we then can use for creating a token.

Thanks @tomzag! This had passed me by completely, a very welcome addition!

Yes, there hasn’t been a big announcement yet. I think we’ve only talked about it on a meeting and in the Slack channel. But since it’s public in the documentation, I guess we can talk about it. :slight_smile:

Hi @Karmalakas I’m just a beginner in UXP, to be honest I’m completely lost in using these methods, unfortunately I couldn’t follow your tips, could you show me a practical example using one of these methods so that I can better understand what you did you try to explain? Thanks for sharing

Hi @tomzag Thanks for the tip! I’m a beginner, still trying to understand the current method, if you can show an example for both that would be great.

@PS-fxrios here’s a repo of a plugin I made to demonstrate using persistent tokens (and storing them for later use) to save a file using batchPlay. Whilst it’s not specifically your use case it has most of the ingredients.

2 Likes

Hi @Timothy_Bennett , thanks for sharing your plugin, it will be very useful. It won’t be easy, but I’ll study and try to understand each line and how to make something so necessary for my workflow work.

We don’t have a lot of informations about it yet. So for example readFile returns an ArrayBuffer. Not sure how we can get a file object that we then can use for creating a token.

Statement from Vinay from the UXP team:

Currently there is no way to get a token from the file after reading using the FileSystem APIs, Provision for this is planned.
FileSystem APIs are to provide unrestricted access, which isn’t currently available with UXP FileSystemProvider without File Picker

3 Likes