
How to get selection using JSScripting?
I have a script that I mentioned before that recolors pixels monochromically. Within an UI mockup I use it to change the states of various elements, icons and text mostly. The script currently works on everything on a layer, and as the mockups are sizeable, the script is too slow. So I have to open the file with the original icon in a new RW window, run the script there and then replace it in the mockup. It would be so much smoother if you gave me the method to get the selection.
Here is the script for your users (it's simple):
"Configuration"
Configuration.AddEditBox("red", "Red Value", "0 - 255",
Configuration.GetValueOrDefault("red", 0));
Configuration.AddEditBox("green", "Green Value", "0 - 255",
Configuration.GetValueOrDefault("green", 0));
Configuration.AddEditBox("blue", "Blue Value", "0 - 255",
Configuration.GetValueOrDefault("blue", 0));
"Execution"
// place your custom JavaScript code here
// the code below changes the color of every pixel
// while leaving alpha
var red = Configuration.GetValue("red");
if (red < 0) red = 0;
if (red > 255) red = 255;
var green = Configuration.GetValue("green");
if (green < 0) green = 0;
if (green > 255) green = 255;
var blue = Configuration.GetValue("blue");
if (blue < 0) blue = 0;
if (blue > 255) blue = 255;
var image = Document.RasterImage;
var sizeX = image.sizeX;
var sizeY = image.sizeY;
for (x=0; x<sizeX; x++)
{
for (y=0; y<sizeY; y++)
{
if (image.GetPixelAlpha(x, y, 0, 0)!= 0)
{
image.SetPixelRed(x, y, 0, 0, red);
image.SetPixelGreen(x, y, 0, 0, green);
image.SetPixelBlue(x, y, 0, 0, blue);
}
}
}