Selection plugin

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)

So, once @Tom walked my team through the main use cases of Photoshop’s C++ SDK, and making a custom selection was in the top four. I think pixel manipulation was, too.

UXP has definitely added more pixel manipulation options over the last two years.

If you can’t accomplish what you need in the JavaScript APIs, there’s always C++.

I thank you for the answer. I imagine that everything can be done with C++ but unfortunately I don’t know and have never used C++ and I don’t know, given my complete inexperience, how sensible it is to embark on the enterprise of creating an entire plugin.

I’d be very interested to know if there’s a way to do this in js and what it is.

Can you help me or do you know someone who can?