How to Insert an Image into the Current Adobe Photoshop Document?

I have successfully downloaded the image to the UXP temporary directory. How can I insert it into the current PS file as a “placed embedded object” or insert it into the current PS file in the form of a layer?

Did you try searching the forum? There’s a topic with a solution

const { fs } = require('uxp').storage.localFileSystem;
const response = await req.get(url, { responseType: "arraybuffer" });
const arrayBuffer = response;
const tempFolder = await uxp.storage.localFileSystem.getTemporaryFolder();
const tempFile = await tempFolder.createFile(`pngpai.com-${item.aid}-${item.title}`, { overwrite: true });
await tempFile.write(arrayBuffer, { format: uxp.storage.formats.binary });

const entry = await fs.getEntryWithUrl(`file:/${tempFile}`);
console.log('entry:', entry)
const sessionToken = await fs.createSessionToken(tempFile);
console.log('sessionToken', sessionToken)

I called both getEntryWithUrl() and createSessionToken, and both of them prompted errors:
TypeError: Cannot read property ‘getEntryWithUrl’ of undefined
TypeError: Cannot read property ‘createSessionToken’ of undefined

Hello, I searched the forum, but I didn’t find a relevant solution.

I don’t know if you’ve tried alchemist,
this doesn’t open a link from the temporary folder
but it opens a file with a personal path
see if you can adapt it to your purpose.



// Please make sure that file system access permission in manifest.json has correct value.

const {executeAsModal} = require("photoshop").core;
const {batchPlay} = require("photoshop").action;
const {localFileSystem: fs} = require("uxp").storage;

async function tokenify(url){
   return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));
}

async function actionCommands() {
   const result = await batchPlay(
      [
         {
            _obj: "placeEvent",
            ID: 160,
            null: {
               _path: await tokenify("/Users/name_user_pc/Desktop/Lego.jpg"),
               _kind: "local"
            },
            linked: true,
            freeTransformCenterState: {
               _enum: "quadCenterState",
               _value: "QCSAverage"
            },
            offset: {
               _obj: "offset",
               horizontal: {
                  _unit: "percentUnit",
                  _value: 0
               },
               vertical: {
                  _unit: "percentUnit",
                  _value: 0
               }
            },
            replaceLayer: {
               _obj: "placeEvent",
               to: {
                  _ref: "layer",
                  _id: 160
               }
            },
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ],
      {}
   );
}

async function open_file() {
   await executeAsModal(actionCommands, {"commandName": "Action Commands"});
}

await open_file();

Hello, friend. According to the code you provided, I integrated it into my project, but it always shows the error: TypeError: fs.getEntryWithUrl is not a function.

.........
const {localFileSystem: fs} = require("uxp").storage;
const tokenify = await fs.createSessionToken(await fs.getEntryWithUrl("file:" + tempFile));
........

There may be a problem in the manifest file,
You have given full disk access.

{
  "id": "com.adobe.uxp.starter.vue3",
  "name": "UXP",
  "version": "1.0.0",
  "main": "index.html",
  "manifestVersion": 4,
  "host": {
    "app": "PS",
    "minVersion": "23.0.0"
  },
  "entrypoints": [
    {
      "type": "panel",
      "id": "helloworld",
      "label": {
        "default": "UXP"
      },
      "minimumSize": { "width": 350, "height": 500 },
      "maximumSize": { "width": 350, "height": 500 },
      "preferredDockedSize": { "width": 230, "height": 300 },
      "preferredFloatingSize": { "width": 230, "height": 300 },
      "icons": [
        {
          "width": 23,
          "height": 23,
          "path": "icons/dark-panel.png",
          "scale": [1, 2],
          "theme": ["darkest", "dark", "medium"],
          "species": ["chrome"]
        },
        {
          "width": 23,
          "height": 23,
          "path": "icons/light-panel.png",
          "scale": [1, 2],
          "theme": ["lightest", "light"],
          "species": ["chrome"]
        }
      ]
    }
  ],
  "icons": [
    {
      "width": 48,
      "height": 48,
      "path": "icons/plugin.png",
      "scale": [1, 2],
      "theme": ["darkest", "dark", "medium", "lightest", "light", "all"],
      "species": ["pluginList"]
    }
  ],
  "requiredPermissions": {
    "localFileSystem": "fullAccess"
  }
}

Update to manifest v5

Maybe you need to update to api 2 too.

Hello 1317048307.
I don’t use the temporary folder, but having the images inside the plugin folder, this example works very well.

async function importPhoto(params) {
  const fs = require("uxp").storage.localFileSystem;
  const doc = app.activeDocument;
  await core.executeAsModal(async () => {	      
      await imprtImg()
       })
       async function imprtImg() {    
        const pluginFolder = await fs.getPluginFolder();
        const theTemplate = await pluginFolder.getEntry("plugin folder/file.jpg");
        const token = await fs.createSessionToken(theTemplate)
        await app.batchPlay([{"_obj":"select","_target":[{"_enum":"ordinal","_ref":"layer","_value":"backwardEnum"}],"layerID":[14],"makeVisible":false},
            {"ID":64,"_obj":"placeEvent","freeTransformCenterState":{"_enum":"quadCenterState","_value":"QCSAverage"},"null":{"_kind":"local","_path":token},"offset":{"_obj":"offset","horizontal":{"_unit":"pixelsUnit","_value":0.0},"vertical":{"_unit":"pixelsUnit","_value":0.0}}},], {});}	
}
1 Like