Hello everyone,
I am still wrestling with my plugin / script
Currently I am following the recommendation from some veterans here on the board, who told me it is better to write the manipulations that I want to perform from script in UXP as a plugin instead of doing it with the different scripts.
So following this idea I have started by reading through the posts and the documentation for the Photoshop API and following into the documentation at:
I did implement a version of flipping a layer that I thought should work.
Codes snippet:
// duplicate the active layer
let duplicatedLayer = await activeLayer.duplicate();
switch (flipName) {
case "horizontal":
await duplicatedLayer.flip.horizontal();
break;
case "vertical":
await duplicatedLayer.flip.vertical();
break;
case "both":
await duplicatedLayer.flip.both();
break;
default:
console.warn("Unknown flip mode:", flipName);
}
Still the function “duplicatedLayer.flip.XXX()” doen’t work. so apparently it is not recognised as a layer object.
Did I overlook something?
The docs have the flip method example wrong. The flip method should be called with the axis as an argument like layer.flip("vertical").
Both the duplicate and flip methods return a promise so they need to be awaited but they also need to be executed inside of a modal state using executeAsModal.
Below is a simple example that includes buttons for each flip type and a single method that does the flipping.
Try it out and let me know if you have any questions?
const { app, core } = require("photoshop");
async function flipLayer(axis = "both") {
const doc = app.activeDocument;
// `duplicate` and `flip` need to execute inside of a modal context
await core.executeAsModal(async () => {
// note that is `doc.activeLayers` is an array
// to only flip the first item I take the 0 index (or only item if there is only one layer selected)
// if you want to flip all layers you can do the dupe and flip actions inside of a for loop
const dupe = await doc.activeLayers[0].duplicate();
dupe.name = `dupe flipped ${axis}`;
dupe.flip(axis);
});
}
document.getElementById("flipLayerHorizontal").addEventListener("click", async () => {
await flipLayer("horizontal");
});
document.getElementById("flipLayerVertical").addEventListener("click", async () => {
await flipLayer("vertical");
});
document.getElementById("flipLayerBoth").addEventListener("click", async () => {
await flipLayer();
});
Hey Josh! (Finally took the time to read in your bio, you are doing a great job by the way)
Thank you for checking back on me! I really love your contribution, it helps me a lot!
The issue that I found is related to the API, it was using the syntax that I implemented.
Thank you so much for clearing this up.
I do not understand how the example would show a different way.
I have currently the implementation of execute as modal in the full function, so after I opened the document, the flipping should occur and then the file should be saved.
I will try to use this information that you have provided to me to implement it in my script.
Thank you again for your help and I will mark your answer as solving my issue once I have implemented it.
Is there a way to contact Adobe to fix their description of the function in the API description?
Cheers,
Oliver (sorry I answered so late to your reply! I was on a business trip with no access to the forum)
Hey @OliverM, glad to hear I could help! As for the incorrect documentation, I submitted an issue in the documentation Github repo. I’m not sure how often they update the docs but at least they know of the error. Let me know if you run into any other issues with your implementation. I’m here to help. Have a great week!
Hey @jduncan Josh, thank you!
Greatly appreciated, it feels a bit like running through a jungle and getting what you need with a machete sometimes .
But we are in for a great experience at least.