How to insert an object using the .psjs format?

How can this JSX script code for inserting objects be implemented using a .psjs format UXP script in Photoshop?

    var desc1 = new ActionDescriptor();
    desc1.putInteger(charIDToTypeID('Idnt'), 4);
    desc1.putPath(charIDToTypeID('null'), new File("~/Desktop/1.jpg"));
    desc1.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), stringIDToTypeID("QCSAverage"));
    var desc2 = new ActionDescriptor();
    desc2.putUnitDouble(charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0);
    desc2.putUnitDouble(charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 1.13686837721616e-13);
    desc1.putObject(charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc2);
    var desc3 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putIdentifier(charIDToTypeID('Lyr '), 4);
    desc3.putReference(charIDToTypeID('T   '), ref1);
    desc1.putObject(stringIDToTypeID("replaceLayer"), charIDToTypeID('Plc '), desc3);
    executeAction(stringIDToTypeID('placeEvent'), desc1, DialogModes.NO);
    //

It’s been multiple times now, that you ask not simply for help, but directly for someone to convert your jsx scripts to psjs, and without showing any effort made by yourself. Did you at least try to convert it? If so, could you share what you did to get it working? What failed?

Sorry, but currently I see your posts as a search for a free labour…

Sorry to make you feel this way, I understand your point of view. I did try to convert it, but encountered some technical issues, perhaps due to my limited understanding and experience. To resolve the problem more effectively, I will do more research and make further attempts, and I will share my progress. If there are any developments, I will share the efforts I have made. Thank you for your patience and understanding, and I apologize again for the inconvenience caused.

const photoshop = require('photoshop');
const { app } = photoshop;

async function placeImage() {
    try {
        let doc = app.activeDocument;
        const filePath = "~/Desktop/1.jpg";

        const placedLayer = await doc.artLayers.add();
        await placedLayer.place(filePath);

        const targetLayer = doc.artLayers[3];
        placedLayer.translate(0, 1.13686837721616e-13);
        placedLayer.move(targetLayer, ElementPlacement.PLACEAFTER);
    } catch (error) {
        console.error("Error placing image:", error);
    }
}

placeImage();

I agree with Karmalaka

you have to try a little harder
I am also a beginner and I try to ask for help only when I can’t find a solution in any way.

Anyway you can use alchemist which is designed specifically to help with uxp programming,
Jarda did a great job.

anyway let’s get to your request
with uxp you need tokens to do what you ask
and the path desktop/1.jpg is not good
in uxp you need the complete path
I use mac the path is the following
/Users/your_name/Desktop/1.jpg

this is the psjs script you can use for your project


// 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: 905,
            null: {
               _path: await tokenify("/Users/your_name/Desktop/1.jpg"),
               _kind: "local"
            },
            freeTransformCenterState: {
               _enum: "quadCenterState",
               _value: "QCSAverage"
            },
            offset: {
               _obj: "offset",
               horizontal: {
                  _unit: "pixelsUnit",
                  _value: 0
               },
               vertical: {
                  _unit: "pixelsUnit",
                  _value: 0
               }
            },
            replaceLayer: {
               _obj: "placeEvent",
               to: {
                  _ref: "layer",
                  _id: 905
               }
            },
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ],
      {}
   );
}

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

await runModalFunction();

2 Likes

Thank you very much for providing the correct code. I really appreciate your help. Before this, I also tried converting it into a psjs code using actions, but it didn’t work properly. Thank you again!

1 Like