Skip to main content
rechmbrs
Inspiring
March 2, 2020
Pregunta

Example of Extendscript PALETTE and colored BUTTONS (revised)

  • March 2, 2020
  • 3 respuestas
  • 4323 visualizaciones

Below is a script for use in Photoshop which uses the window type "palette".  It is opened by locating the jsx file in File>Scripts>...   It will remain open until closed by the user even while applying operations to an image.  The script also demonstrates a way of creating rectangular buttons with colored background and text.

 

Thanks to r-bin and Kukurykus for supplying the information for building this script.

 

This script can  be used as input for creating a jsxbin file.  To create a jsxbin file, remove the last six lines of code (BridgeTalk lines) and run save binary in ESTK.   With the jsxbin file, run the code from r-bin below in the thread to run the jsxbin file.

 

 

 

 

 

//#script "palette3.jsx"
//@target photoshop
/* 
    This routine is a combined effort based on information supplied 
    by r-bin and Kukurykus of Adobe Community.
*/

paletteFunction();
function paletteFunction()
{
    var paletteTitle = "RONC 02Mar2020";

    var winTest, win;
    var bridgeTalk, photoShop;

    var b1, b2, b3;
    var g0, g1, g2, g3;

    try
    {
        winTest = Window.find("palette", paletteTitle);
        if (winTest)
        {
            beep();
            winTest.show();
            return;
        }

        win = new Window("palette", paletteTitle);
        win.spacing = 0;
        win.margins = 0;

        var font = ScriptUI.newFont("Arial", ScriptUI.FontStyle.BOLD, 20);

        g0 = win.add("group");
        g0.orientation = "row";
        g0.alignment = "center";
        g0.spacing = 0;
        g0.margins = 0;


        g1 = g0.add("group");
        g1.spacing = 0;
        g1.margins = 0;
        g1.preferredSize = [150, 60];

        g1.graphics.backgroundColor = g1.graphics.newBrush(g1.graphics.BrushType.SOLID_COLOR, [1, 0, 0, 1]);
        b1 = g1.add("statictext", undefined, "Test1");
        b1.justify = "center";
        b1.alignment = ["fill", "fill"];
        b1.graphics.foregroundColor = b1.graphics.newPen(b1.graphics.PenType.SOLID_COLOR, [0, 1, 0, 1], 1);
        b1.graphics.font = font;
        b1.onClick = function () { alert(this.text); };

        g3 = g0.add("group");
        g3.spacing = 0;
        g3.margins = 0;
        g3.preferredSize = [150, 60];

        g3.graphics.backgroundColor = g3.graphics.newBrush(g3.graphics.BrushType.SOLID_COLOR, [1, 1, 0, 1]);
        b3 = g3.add("statictext", undefined, "Test3");
        b3.justify = "center";
        b3.alignment = ["fill", "fill"];
        b3.graphics.foregroundColor = b3.graphics.newPen(b3.graphics.PenType.SOLID_COLOR, [0, 1, 1, 1], 1);
        b3.graphics.font = font;
        b3.onClick = function () { alert(this.text); };


        g2 = win.add("group");
        g2.spacing = 0;
        g2.margins = 0;
        g2.preferredSize = [150, 60];

        g2.graphics.backgroundColor = g2.graphics.newBrush(g2.graphics.BrushType.SOLID_COLOR, [0, 0, 1, 1]);
        b2 = g2.add("statictext", undefined, "Test2 - Close");
        b2.justify = "center";
        b2.alignment = ["fill", "fill"];
        b2.graphics.foregroundColor = b2.graphics.newPen(b2.graphics.PenType.SOLID_COLOR, [1, 1, 0, 1], 1);
        b2.graphics.font = font;
        b2.onClick = function () { alert(this.text); win.close(); };

        win.show();
    }
    catch (e)
    {
        alert(e);
    }
}

bridgeTalk = new BridgeTalk();

var photoShop = BridgeTalk.getSpecifier("photoshop");

if (!photoShop) photoShop = "photoshop-60.064"; // need redone
bridgeTalk.target = photoShop;

bridgeTalk.body = "var f=" + paletteFunction.toSource() + ";f();";
bridgeTalk.send();

 

 

 

This is what the "palette and colored buttons" should look like:

RONC

 

Este tema ha sido cerrado para respuestas.

3 respuestas

Participant
March 4, 2020

ใส่มาเยอะๆ

Legend
March 2, 2020
Make all the main code in a separate file. Compile it in JSXBIN. Place the JSXBIN file in the same folder as the main script.

Use this code.
 

 

 

 

var bt = new BridgeTalk();

var ph = BridgeTalk.getSpecifier("photoshop");

if (!ph) ph = "photoshop-60.064" // only for CS6x64, need redone
bt.target = ph;

var jsxbin = "aaaa.jsxbin"; // your jsxbin name here

var pth = new File($.fileName).parent.fullName + "/" + jsxbin;
bt.body = "$.evalFile(" + pth.toSource() + ")"; 
bt.send();

 

 

 

P.S. JSXBIN will not save you from hackers. ))

rechmbrs
Inspiring
March 3, 2020

r-bin,

I removed the 6 lines related to BridgeTalk at bottom of original source above save as palette3.jsx and created a jsxbin file from that source.  Plugged the name of the file palette3.jsxbin into your code from above (see below also) and saved as paletteBT.jsx.  I ran that file from Photoshop and nothing happened.

 

Your code with my changes:

var bt = new BridgeTalk();

var ph = BridgeTalk.getSpecifier("photoshop");

if (!ph) ph = "photoshop-60.064" // only for CS6x64, need redone
bt.target = ph;

var jsxbin = "palette3.jsxbin"; // your jsxbin name here

var pth = new File($.fileName).parent.fullName + "/" + jsxbin;

//var pth = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/palette3.jsxbin";
alert(pth);
bt.body = "$.evalFile(" + pth.toSource() + ")";
alert(bt.body);
bt.send();
/*

location  of  jsxbin:
C:\Users\RECHM\Dropbox\$$-SYNC\$$-Scripts\__SC-Save\palette3.jsxbin

---------------------------
pth  Script Alert
---------------------------
~/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/palette3.jsxbin
---------------------------
OK
---------------------------

---------------------------
bt.body  Script Alert  try with actual code
---------------------------
$.evalFile((new String("~/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/palette3.jsxbin")))
---------------------------
OK
---------------------------

---------------------------
---------------------------
bt.body  Script Alert  try with inputing complete address.
---------------------------
$.evalFile((new String("C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/palette3.jsxbin")))
---------------------------
OK
---------------------------


*/

 I tried two ways.  First just using your code and the filename and a second where I put the complete path and filename and both did nothing.

 

Did I need to remove more that the last lines for making the jsxbin?

 

I'm lost.

RONC

Legend
March 3, 2020

>>>I removed the 6 lines related to BridgeTalk at bottom of original source above

>>>save as palette3.jsx and created a jsxbin file from that source.

>>>Plugged the name of the file palette3.jsxbin into your code from above (see below

>>>also) and saved as paletteBT.jsx. I ran that file from Photoshop and nothing happened.

 

Did you insert a function call into this new script? )

 

 

paletteFunction();

 

JJMack
Community Expert
Community Expert
March 2, 2020

I like to position dialogs not centered so I added a littlt code to your script to position the dialog foe  ny 1K display.

//#script "palette3.jsx"
//@target photoshop
/* 
    This routine is a combined effort based on information supplied 
    by r-bin and Kukurykus of Adobe Community.
*/

function paletteFunction()
{
    var paletteTitle = "RONC 02Mar2020";

    var winTest, win;
    var bridgeTalk, photoShop;

    var b1, b2, b3;
    var g0, g1, g2, g3;

    try
    {
        winTest = Window.find("palette", paletteTitle);
        if (winTest)
        {
            beep();
			winTest.onShow = function() {
				var ww = winTest.bounds.width;  
				var hh = winTest.bounds.height;  
				winTest.bounds.x  = 1870 - ww;  
				winTest.bounds.y  = 100;  
				winTest.bounds.width  = ww;  
				winTest.bounds.height  = hh;  
			}	
            winTest.show();
            return;
        }

        win = new Window("palette", paletteTitle);
        win.spacing = 0;
        win.margins = 0;

        var font = ScriptUI.newFont("Arial", ScriptUI.FontStyle.BOLD, 20);

        g0 = win.add("group");
        g0.orientation = "row";
        g0.alignment = "center";
        g0.spacing = 0;
        g0.margins = 0;


        g1 = g0.add("group");
        g1.spacing = 0;
        g1.margins = 0;
        g1.preferredSize = [150, 60];

        g1.graphics.backgroundColor = g1.graphics.newBrush(g1.graphics.BrushType.SOLID_COLOR, [1, 0, 0, 1]);
        b1 = g1.add("statictext", undefined, "Test1");
        b1.justify = "center";
        b1.alignment = ["fill", "fill"];
        b1.graphics.foregroundColor = b1.graphics.newPen(b1.graphics.PenType.SOLID_COLOR, [0, 1, 0, 1], 1);
        b1.graphics.font = font;
        b1.onClick = function () { alert(this.text); };

        g3 = g0.add("group");
        g3.spacing = 0;
        g3.margins = 0;
        g3.preferredSize = [150, 60];

        g3.graphics.backgroundColor = g3.graphics.newBrush(g3.graphics.BrushType.SOLID_COLOR, [1, 1, 0, 1]);
        b3 = g3.add("statictext", undefined, "Test3");
        b3.justify = "center";
        b3.alignment = ["fill", "fill"];
        b3.graphics.foregroundColor = b3.graphics.newPen(b3.graphics.PenType.SOLID_COLOR, [0, 1, 1, 1], 1);
        b3.graphics.font = font;
        b3.onClick = function () { alert(this.text); };


        g2 = win.add("group");
        g2.spacing = 0;
        g2.margins = 0;
        g2.preferredSize = [150, 60];

        g2.graphics.backgroundColor = g2.graphics.newBrush(g2.graphics.BrushType.SOLID_COLOR, [0, 0, 1, 1]);
        b2 = g2.add("statictext", undefined, "Test2 - Close");
        b2.justify = "center";
        b2.alignment = ["fill", "fill"];
        b2.graphics.foregroundColor = b2.graphics.newPen(b2.graphics.PenType.SOLID_COLOR, [1, 1, 0, 1], 1);
        b2.graphics.font = font;
        b2.onClick = function () { alert(this.text); win.close(); };


        win.show();
    }
    catch (e)
    {
        alert(e);
    }
}

bridgeTalk = new BridgeTalk();

var photoShop = BridgeTalk.getSpecifier("photoshop");

if (!photoShop) photoShop = "photoshop-60.064"; // need redone
bridgeTalk.target = photoShop;

bridgeTalk.body = "var f=" + paletteFunction.toSource() + ";f();";
bridgeTalk.send();
JJMack
rechmbrs
Inspiring
March 3, 2020

JJ,

Thanks for the addition. 

 

I intentionally didn't put anything in my code that doesn't relate directly to the "palette and colored buttons" so that those people trying to learn and use the code don't have to wade through extraneous code.

 

RONC