Image path on my PC is fine but not on another

Yesterday I made the mip plugin open a file with insert embedded in photoshop, but I noticed a problem and I don’t know how to solve it
in the file I have to insert the path of the file which is this
_path: await tokenify(“/Users/username/Desktop/Leo.jpg”),
this is fine for my pc
but if I use it on another PC I have to change the username and this is a limit for me
I used this in the past to work around the problem
~/Desktop/Leo.jpg
but I saw that it doesn’t work
Where am I dazzling?

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

const app = require('photoshop').app;


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

async function importLayer() {
   const result = await batchPlay(
      [
         {
            _obj: "placeEvent",
            null: {
               _path: await tokenify("/Users/nameuser/Desktop/Leo.jpg"),
               _kind: "local"
            },
            freeTransformCenterState: {
               _enum: "quadCenterState",
               _value: "QCSAverage"
            },
            offset: {
               _obj: "offset",
               horizontal: {
                  _unit: "pixelsUnit",
                  _value: 0
               },
               vertical: {
                  _unit: "pixelsUnit",
                  _value: 0
               }
            },
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ],
      {}
   );
}

    await executeAsModal(importLayer, {commandName: "importLayer"});

Here’s one approach, but it uses a file picker.
Otherwise you could use fs.getPluginFolder() and extract the username from the nativePath.

The path of the user’s home directory can be obtained and used in this way.

const os = require('os') ;
const imgPath = path.join(os.homedir(), 'Desktop', 'Leo.jpg') ;
3 Likes

Thank you both for your help, I’ll try later and see if I can.

Sorry if I only answer now, the script works perfectly.

Thank you

1 Like