Script that copies the selected number of pixels
Hi guys, I've been trying to make this script for a while and I can't get the result I need.
I'm trying to make a script that copies the amount of selected pixels to the clipboard, but it doesn't give the same value that appears in the histogram, can anyone help me?

#target photoshop
// Get the current Photoshop document
var doc = app.activeDocument;
// Get the brightness (Y) channel of the image
var channel = doc.activeChannels[0];
// Get the histogram of the luminosity channel
var histogram = channel.histogram;
// Get the total sum of histogram values
var totalPixelCount = 0;
for (var i = 0; i < histogram.length; i++) {
totalPixelCount += histogram[i];
}
// Get the selected pixel amount value from the histogram
var pixelCount = totalPixelCount - histogram[0];
// Copy the value to the clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), totalPixelCount);
executeAction(stringIDToTypeID("textToClipboard"), d, DialogModes.NO);
// Show an alert
alert("Pixels: " + totalPixelCount);

