I cannot for the life of me figure this one out. This is my 2nd or 3rd attempt and I am about to give up again.
I would like to be able to have some coordinates and create a shape layer from them. I am trying to recreate some custom shapes.
I tried the example code here with no luck:
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/pathpointinfo/
Perhaps it is because I am not using typescript, but I tried (and chatgpt tried) to convert it and still had no luck.
I tried to do it via batchplay and get an error that Make is not available. I tried both in and out of modal with no luck.
Here is one of the attempts I did with chatgpt:
const batchPlay = require('photoshop').action.batchPlay;
async function createPath(start, stop) {
try {
const app = require('photoshop').app;
const activeDocument = app.activeDocument;
if (!activeDocument) {
throw new Error("No active document found.");
}
console.log("Active document ID: ", activeDocument.id);
const activeLayers = activeDocument.activeLayers;
if (!activeLayers || !activeLayers.length) {
throw new Error("No active layer found.");
}
const activeLayerID = activeLayers[0].id;
console.log("Active layer ID: ", activeLayerID);
// Ensure no open modal states
await batchPlay([{
_obj: 'close',
_target: [{ _ref: 'dialog', _enum: 'ordinal', _value: 'all' }]
}], { synchronousExecution: true });
// Switch to Standard mode (to avoid any modal states)
await batchPlay([{
_obj: 'modalState',
_value: 0
}], { synchronousExecution: true });
const startPoint = {
_obj: 'pathPoint',
anchor: start,
leftDirection: start,
rightDirection: start,
kind: { _enum: 'pointKind', _value: 'cornerPoint' }
};
const stopPoint = {
_obj: 'pathPoint',
anchor: stop,
leftDirection: stop,
rightDirection: stop,
kind: { _enum: 'pointKind', _value: 'cornerPoint' }
};
const subPath = {
_obj: 'subPath',
operation: { _enum: 'shapeOperation', _value: 'add' },
closedSubpath: false,
points: [startPoint, stopPoint]
};
const pathDescriptor = {
_obj: 'make',
new: { _class: 'path' },
from: {
_obj: 'path',
name: 'Line',
subpaths: [subPath],
kind: { _enum: 'pathKind', _value: 'workPath' }
}
};
console.log("Creating path...");
const result = await batchPlay([pathDescriptor], {
synchronousExecution: true,
modalBehavior: 'execute'
});
const setDescriptor = {
_obj: 'set',
_target: [{ _ref: 'path', _enum: 'ordinal', _value: 'targetEnum' }],
to: { _name: 'Line' }
};
const namingResult = await batchPlay([setDescriptor], {
synchronousExecution: true,
modalBehavior: 'execute'
});
console.log("Path creation result: ", result);
console.log("Path naming result: ", namingResult);
console.log("Path created successfully.");
} catch (error) {
console.error("Error creating path:", error.message);
}
}
// Execute the function
(async () => {
try {
await createPath([100, 100], [200, 200]);
} catch (error) {
console.error("Error in execution: ", error);
}
})();
Any help would be appreciated.