Good morning everyone,
I apologize if my questions seem trivial but I’m not an expert and I found little documentation and few topics on the subject. I would like this to be an opportunity to clarify the topic on the forum and give a clear answer to some questions that for amateurs like me are not obvious about photoshop plugins and selections using UXP.
Premise:
I’m making a photoshop plugin using UXP. The main purpose of the plugin is to return a selection to the user. I would like to develop it in two stages.
-
Phase 1: I would like to create a plugin consisting of a panel with a button. On click the user must obtain a selection of the whole document. (As if running command+A)
-
Phase two: I would like to add a numeric input to the panel with a minimum value of 0 and a maximum of 255. On click, the user must obtain a selection of pixels with luminosity equal to or greater than the input value.
Development and problems:
I tried to approach the problem with both a simple js plugin and a react plugin. I have read the documentation related to selections that can be found in the photoshop API (Classes > Selection).
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/selection/
I’m trying to execute selectAll() with something like :
const app = require('photoshop').app;
console.log("App: ", app)
const doc = await app.activeDocument;
console.log("Doc: ", doc)
const sel = await doc.selection.selectAll();
console.log("Sel: ", sel)
However I only get the first two console.logs
App: Photoshop {…}
Doc: Document {…}
If I try to run the command in the console
require('photoshop').app.activeDocument.selection
i get
undefined
If I try to run the command in the console
require('photoshop').app.activeDocument.selection.selectAll()
i get
Uncaught TypeError: Cannot read properties of undefined (reading 'selectAll') at <anonymous>:1:50
I read somewhere that to get and return a selection you have to use Alchemist/batchPlay, is that true?. How can you structure a minimal plugin with a panel and a button that returns the selection of all the pixels to the user when clicked?
The index.js file follows. Attached you will find the entire plugin folder.
const app = require('photoshop').app;
console.log("App: ", app)
// Define the button function to be executed as a modal operation
async function buttonFunction() {
const doc = await app.activeDocument;
console.log("Doc: ", doc)
const sel = await doc.selection.selectAll();
console.log("Sel: ", Sel)
}
// Button click event listener
document.getElementById('button').addEventListener('click', async () => {
try {
// Execute the button function as a modal operation
await require('photoshop').core.executeAsModal(buttonFunction, {"commandName": "My Script Command"});
} catch (e) {
if (e.number == 9) {
showAlert("executeAsModal was rejected (some other plugin is currently inside a modal scope)");
} else {
// This case is hit if the targetFunction throws an exception
}
}
});
Attached you will find the entire plugin folder.
test_1.zip (11.0 KB)