How to place image from file system in UXP Indesign Script

I am surprised this is so hard. No recipe in the docs seems to cover this. Here’s roughly what I am trying to do:

  const imageFrame = targetPage.rectangles.add();
  const imageFile = fs.readFileSync(`${DATA_FOLDER_PATH}/${imageFileName}`);
  const placedImage = imageFrame.place(imageFile);
  imageFrame.geometricBounds = [yStart, xStart, yEnd, xEnd]; // etc

The error alert says that the first argument to place isn’t being supplied. This isn’t quite right, of course. There is an argument. The previous line is executing as expected. In the UXP console I can see that the file is being located in the file system. imageFile is an ArrayBuffer so it’s not that.

I’ve also tried using fs.openSync but I get an alert saying that doesn’t exist. It doesn’t seem right, either, to pass an fd into place. How do I pass the file into place?

Thanks in advance for any help. This is a dupe of Placing a graphic from the local file system in In... - Adobe Community - 14661606

The place method takes an actual file handle as parameter, not a file content.
So doing something like:

const placedImage=imageFrame.place(`${DATA_FOLDER_PATH}/${imageFileName}`);

Should work

@vamitul Thanks, that was the first thing that I tried. But when I did that, I got the error: “Cannot create the link resource from the given URI.”

I will say that this feels like a pathing issue of some sort. The concatenated folder path and file path is something like:

Users/myAccount/Documents/a-folder/another-folder/My file-01.tif

The path is “conventionally” correct according to Node and my system (Mac OS). When I readFileSync using this path, the file is found. So why can’t the script find it when I pass the exact same path to place? Are there escaping rules that I need to be aware of? :thinking:

Ok, I figured it out. UXP is expecting a leading slash on the file path, where the Node-style methods do mnot require this. So this:

/Users/myAccount/Documents/a-folder/another-folder/My file-01.tif

works.

1 Like