Skip to main content
Participant
November 13, 2017
Answered

Scripting Circles with JS (Photoshop CC 2017.0.1)

  • November 13, 2017
  • 2 replies
  • 2248 views

Hi,

I'm not finding any good examples to help me write a function to draw Circles in Photoshop CC with JavaScript.

I want to provide:

- Radius (or Diameter), in pixels

- Center Point (x,y), in pixels

- Stroke Width, in pixels

- Fill Color (I'm not sure what's the best here, words, or Hex?)

Something like:

Circle( 100, 200, 300, 5, 'White')

Most examples I'm seeing are for older versions and no longer work, or are overly complex.

Any simple solutions floating around?

Thank you!

This topic has been closed for replies.
Correct answer r-bin

// test this sript

// examples of a function call

circle(100, 200, 300, 3, "Red");

//circle(100, 200, 300, 3, "yellow");

//circle(100, 200, 300, 3, "#0190F7");

//circle(100, 200, 300, 3, 100, 150, 180);

////////////////////////////////////////////////////////////////////////////////////////////////////////

function circle(r, x, y, w, c_r, c_g, c_b)

    {

    try

        {

        select_layer_rgb();

      

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var r1 = new ActionReference();

        r1.putProperty( charIDToTypeID( "Path" ), charIDToTypeID( "WrPt" ) );

        d1.putReference( charIDToTypeID( "null" ), r1 );

        d2.putUnitDouble( charIDToTypeID( "Top " ), charIDToTypeID( "#Pxl" ), y-r );

        d2.putUnitDouble( charIDToTypeID( "Left" ), charIDToTypeID( "#Pxl" ), x-r );

        d2.putUnitDouble( charIDToTypeID( "Btom" ), charIDToTypeID( "#Pxl" ), y+r );

        d2.putUnitDouble( charIDToTypeID( "Rght" ), charIDToTypeID( "#Pxl" ), x+r );

        d1.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Elps" ), d2 );

        executeAction( charIDToTypeID( "setd" ), d1, DialogModes.NO );

        d1 = null;

        d2 = null;

        r1 = null;

        var idx = curr_path_idx();

        app.activeDocument.pathItems[idx].makeSelection(0, true, SelectionType.REPLACE);

        app.activeDocument.pathItems[idx].remove();

        var c = new SolidColor();

        with (c.rgb) { red = green = blue = 0; }

        if (c_r != undefined)

            {

            if (typeof(c_r) == "string")

                {

                if (c_r.indexOf("#") == 0)

                    {

                    c.rgb.hexValue = c_r.substr(1);

                    }

                else

                    {                  

                    switch (c_r.toLowerCase())

                        {

                        case "red":     c.rgb.red = 255; c.rgb.green=0;   c.rgb.blue=0;   break;

                        case "green":   c.rgb.red = 0;   c.rgb.green=255; c.rgb.blue=0;   break;

                        case "blue":    c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=255; break;

                        case "yellow":  c.rgb.red = 255; c.rgb.green=255; c.rgb.blue=0;   break;

                        case "magenta": c.rgb.red = 255; c.rgb.green=0;   c.rgb.blue=255; break;

                        case "cyan":    c.rgb.red = 0;   c.rgb.green=255; c.rgb.blue=255; break;

                        case "black":   c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=0;   break;

                        case "white":   c.rgb.red = 255; c.rgb.green=255; c.rgb.blue=255; break;

                        case "gray":    c.rgb.red = 128; c.rgb.green=128; c.rgb.blue=128; break;

                        default:        c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=0;   break;

                        }

                    }

                }

            else if (typeof(c_r) == "number")

                {

                c.rgb.red   = c_r;

                c.rgb.green = c_g; 

                c.rgb.blue  = c_b;

                }  

            }

        stroke(w, c.rgb.red, c.rgb.green, c.rgb.blue);

        app.activeDocument.selection.deselect();

        }

    catch (e) { alert(e); }

    }

////////////////////////////////////////////////////////////////////////////////////////////

function select_layer_rgb()

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "RGB " ) );

        d.putReference( charIDToTypeID( "null" ), r );

        d.putBoolean( charIDToTypeID( "MkVs" ), false );

        executeAction( charIDToTypeID( "slct" ), d, DialogModes.NO );

        r = null;

        d = null;  

        }

    catch (e) { alert(e);  }

    }

////////////////////////////////////////////////////////////////////////////////////////////

function curr_path_idx()

    {

    try {

        var r = new ActionReference();

        r.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "targetPathIndex" ) );

        r.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        r = executeActionGet(r);

        return  r.getInteger( stringIDToTypeID( 'targetPathIndex' ));

        }

    catch (e) { alert(e); return -1; }

    }

///////////////////////////////////////////////////////////////////////////////////////////////////

function stroke(size, r, g, b)

    { 

    try {

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        d1.putInteger( charIDToTypeID( "Wdth" ), size );

        d1.putEnumerated( charIDToTypeID( "Lctn" ), charIDToTypeID( "StrL" ), charIDToTypeID( "Cntr" ) );

        d1.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), 100.000000 );

        d1.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );

        d2.putDouble( charIDToTypeID( "Rd  " ), r );

        d2.putDouble( charIDToTypeID( "Grn " ), g );

        d2.putDouble( charIDToTypeID( "Bl  " ), b );

        d1.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), d2 );

        executeAction( charIDToTypeID( "Strk" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    }

2 replies

r-binCorrect answer
Legend
November 13, 2017

// test this sript

// examples of a function call

circle(100, 200, 300, 3, "Red");

//circle(100, 200, 300, 3, "yellow");

//circle(100, 200, 300, 3, "#0190F7");

//circle(100, 200, 300, 3, 100, 150, 180);

////////////////////////////////////////////////////////////////////////////////////////////////////////

function circle(r, x, y, w, c_r, c_g, c_b)

    {

    try

        {

        select_layer_rgb();

      

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var r1 = new ActionReference();

        r1.putProperty( charIDToTypeID( "Path" ), charIDToTypeID( "WrPt" ) );

        d1.putReference( charIDToTypeID( "null" ), r1 );

        d2.putUnitDouble( charIDToTypeID( "Top " ), charIDToTypeID( "#Pxl" ), y-r );

        d2.putUnitDouble( charIDToTypeID( "Left" ), charIDToTypeID( "#Pxl" ), x-r );

        d2.putUnitDouble( charIDToTypeID( "Btom" ), charIDToTypeID( "#Pxl" ), y+r );

        d2.putUnitDouble( charIDToTypeID( "Rght" ), charIDToTypeID( "#Pxl" ), x+r );

        d1.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Elps" ), d2 );

        executeAction( charIDToTypeID( "setd" ), d1, DialogModes.NO );

        d1 = null;

        d2 = null;

        r1 = null;

        var idx = curr_path_idx();

        app.activeDocument.pathItems[idx].makeSelection(0, true, SelectionType.REPLACE);

        app.activeDocument.pathItems[idx].remove();

        var c = new SolidColor();

        with (c.rgb) { red = green = blue = 0; }

        if (c_r != undefined)

            {

            if (typeof(c_r) == "string")

                {

                if (c_r.indexOf("#") == 0)

                    {

                    c.rgb.hexValue = c_r.substr(1);

                    }

                else

                    {                  

                    switch (c_r.toLowerCase())

                        {

                        case "red":     c.rgb.red = 255; c.rgb.green=0;   c.rgb.blue=0;   break;

                        case "green":   c.rgb.red = 0;   c.rgb.green=255; c.rgb.blue=0;   break;

                        case "blue":    c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=255; break;

                        case "yellow":  c.rgb.red = 255; c.rgb.green=255; c.rgb.blue=0;   break;

                        case "magenta": c.rgb.red = 255; c.rgb.green=0;   c.rgb.blue=255; break;

                        case "cyan":    c.rgb.red = 0;   c.rgb.green=255; c.rgb.blue=255; break;

                        case "black":   c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=0;   break;

                        case "white":   c.rgb.red = 255; c.rgb.green=255; c.rgb.blue=255; break;

                        case "gray":    c.rgb.red = 128; c.rgb.green=128; c.rgb.blue=128; break;

                        default:        c.rgb.red = 0;   c.rgb.green=0;   c.rgb.blue=0;   break;

                        }

                    }

                }

            else if (typeof(c_r) == "number")

                {

                c.rgb.red   = c_r;

                c.rgb.green = c_g; 

                c.rgb.blue  = c_b;

                }  

            }

        stroke(w, c.rgb.red, c.rgb.green, c.rgb.blue);

        app.activeDocument.selection.deselect();

        }

    catch (e) { alert(e); }

    }

////////////////////////////////////////////////////////////////////////////////////////////

function select_layer_rgb()

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "RGB " ) );

        d.putReference( charIDToTypeID( "null" ), r );

        d.putBoolean( charIDToTypeID( "MkVs" ), false );

        executeAction( charIDToTypeID( "slct" ), d, DialogModes.NO );

        r = null;

        d = null;  

        }

    catch (e) { alert(e);  }

    }

////////////////////////////////////////////////////////////////////////////////////////////

function curr_path_idx()

    {

    try {

        var r = new ActionReference();

        r.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "targetPathIndex" ) );

        r.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        r = executeActionGet(r);

        return  r.getInteger( stringIDToTypeID( 'targetPathIndex' ));

        }

    catch (e) { alert(e); return -1; }

    }

///////////////////////////////////////////////////////////////////////////////////////////////////

function stroke(size, r, g, b)

    { 

    try {

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        d1.putInteger( charIDToTypeID( "Wdth" ), size );

        d1.putEnumerated( charIDToTypeID( "Lctn" ), charIDToTypeID( "StrL" ), charIDToTypeID( "Cntr" ) );

        d1.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), 100.000000 );

        d1.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );

        d2.putDouble( charIDToTypeID( "Rd  " ), r );

        d2.putDouble( charIDToTypeID( "Grn " ), g );

        d2.putDouble( charIDToTypeID( "Bl  " ), b );

        d1.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), d2 );

        executeAction( charIDToTypeID( "Strk" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    }

Participant
November 13, 2017

Thank you, that works wonderfully.  I appreciate the help!

Participating Frequently
November 13, 2017

!

You have to create a function yourself with the scriptlistener of CC - the circles functions that you will find have been created with other versions of Photoshop like this one by Paul MR which works with CS5 :

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

Circle(0,0,100,300);

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

function Circle(Top,Left,Bottom,Right) {

var desc3 = new ActionDescriptor();

        var ref1 = new ActionReference();

        ref1.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );

    desc3.putReference( charIDToTypeID('null'), ref1 );

        var desc4 = new ActionDescriptor();

        desc4.putUnitDouble( charIDToTypeID('Top '), charIDToTypeID('#Pxl'), Top );

        desc4.putUnitDouble( charIDToTypeID('Left'), charIDToTypeID('#Pxl'), Left );

        desc4.putUnitDouble( charIDToTypeID('Btom'), charIDToTypeID('#Pxl'), Bottom );

        desc4.putUnitDouble( charIDToTypeID('Rght'), charIDToTypeID('#Pxl'), Right );

    desc3.putObject( charIDToTypeID('T   '), charIDToTypeID('Elps'), desc4 );

    desc3.putBoolean( charIDToTypeID('AntA'), true );

    executeAction( charIDToTypeID('setd'), desc3, DialogModes.NO );

};