How can I import an image or a file in Batchplay?

How can I import an image or a file in Batchplay? I want to import a file available locally in the plugin folder to the open project in Photoshop.
I started with this but it doesn’t work …

const batchPlay = require(“photoshop”).action.batchPlay;
async function Import() {
const result = await batchPlay(
_obj: “placeEvent”,
ID: 1671,
null: {
_path: “C:\Users\me\Downloads\MyPlugin\White.png”,
_kind: “local”
freeTransformCenterState: {
_enum: “quadCenterState”,
_value: “QCSAverage”
offset: {
_obj: “offset”,
horizontal: {
_unit: “pixelsUnit”,
_value: 0
vertical: {
_unit: “pixelsUnit”,
_value: 0
_options: {
dialogOptions: “dontDisplay”
synchronousExecution: false,
modalBehavior: “fail”

Thank you in advance for your answers

Accessing files via paths won’t work in UXP. For security reasons, you need to create a token of that file entry first (check the docs) and pass that one to the _path key

UXP requires the user to manually allow access to the file you want to import. That can either be done directly by letting the user select the file:

const fs = require(‘uxp’).storage.localFileSystem;

const file = await fs.getFileForOpening()

or, if you want to have your program select the file(s), by selecting a directory tree containing said file(s):

const folder = await fs.getFolder()

To get all the files on the selected level you could do something like that:

const entries = await folder.getEntries();
const files = entries.filter( entry => entry.isFile );

To be able to provide a string to set for the path:

_path: “C:\Users\me\Downloads\MyPlugin\White.png”,

you can create a token for the file in question:

const token = fs.createSessionToken( file );

and replace the above line by:

_path: token

1 Like

Thanks a lot ! It helped me so much

1 Like