Skip to main content
Inspiring
November 14, 2024
Question

Script (JS) : selecting an ellipse

  • November 14, 2024
  • 2 replies
  • 480 views

Hi all

 

I am trying to select an ellipse and make that selection workable. It is straight forward for a rectangle :

 

function select_rect(source_img, left, top, right, bottom) {
    source_img.selection.select([
        [left, top],
        [right, top],
        [right, bottom],
        [left, bottom]
    ]);
    select_rect=source_img.selection;
}

 

For an ellipse selection, I found this code :

function select_Ellipse(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 );
}

But the selection is not returned in the function like it is with my select_rect function.

I would like the select_ellipse to be in the form of :

 

function select_ellipse(sourceImg, left,top,right,bottom,antiAlias) {
    [... code ...]
    select_ellipse = ???
}

 

Is it possible ? How to do this ?

 

Clear sky

 

Fred

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
November 15, 2024

An elliptical selection to the canvas edges here:

 

https://gist.github.com/MarshySwamp/451f60096079084b818d693d8cf09547

Participant
November 18, 2024

Thank you Stephen, your code is more optimized than the one I used.

 

My script can now play with the selection I made to make rectangles, ellipses, slanted quadrigones... Good for timeslices !

Legend
November 14, 2024
select_rect=source_img.selection;
What is this code for???
You should never do this!
It can cause the program to crash.
 

 
Fre5D95Author
Inspiring
November 14, 2024

But it works ! And I never had any crash doing like that...

 

Maybe it could be better to return like :

return source_img.selection;