Search code examples
javascriptphotoshopellipsephotoshop-script

How do I create a circular or elliptical selections in Javascript for use in Photoshop?


So my end goal is to create 4 circular selections at the corners, but after looking through documentation and having searched many times, I couldn't find information on anything other than rectangular selection areas. I believe there should be a solution, but I can't really think of anything that doesn't involve setting up a huge array of boundary pixel locations for a circle (which I'm not even sure would work, as I'm fairly new to Photoshop scripting and how it works).

Many thanks!


Solution

  • The Photoshop scripting API in not complete and sometimes you will get stuck trying to accomplish certain tasks. A great tool for handling this is Adobe's very own ScriptListener plugin. It will take a little bit of time to figure out the code it spits out but this will help fill the holes with your scripting. Below is a function I use for making circular selections:

    function makeCircle(left,top,right,bottom,antiAlias){
    
    var circleSelection = charIDToTypeID( "setd" );
        var descriptor = new ActionDescriptor();
        var id71 = charIDToTypeID( "null" );
            var ref5 = new ActionReference();
            var id72 = charIDToTypeID( "Chnl" );
            var id73 = charIDToTypeID( "fsel" );
            ref5.putProperty( id72, id73 );
        descriptor.putReference( id71, ref5 );
        var id74 = charIDToTypeID( "T   " );
            var desc12 = new ActionDescriptor();
    
            var top1 = charIDToTypeID( "Top " );
            var top2 = charIDToTypeID( "#Pxl" );
            desc12.putUnitDouble( top1, top2, top );
    
            var left1 = charIDToTypeID( "Left" );
            var left2 = charIDToTypeID( "#Pxl" );
            desc12.putUnitDouble( left1, left2, left );
    
            var bottom1 = charIDToTypeID( "Btom" );
            var bottom2 = charIDToTypeID( "#Pxl" );
            desc12.putUnitDouble( bottom1, bottom2, bottom );
    
            var right1 = charIDToTypeID( "Rght" );
            var right2 = charIDToTypeID( "#Pxl" );
            desc12.putUnitDouble( right1, right2, right );
    
        var id83 = charIDToTypeID( "Elps" );
        descriptor.putObject( id74, id83, desc12 );
        var id84 = charIDToTypeID( "AntA" );
        descriptor.putBoolean( id84, antiAlias );
    executeAction( circleSelection, descriptor, DialogModes.NO );
    }