Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
4

Creating Circle with Photoshop Script

Community Beginner ,
Jan 14, 2024 Jan 14, 2024

I dont know why but I always ran into error when trying to add shape
never ran into any problem when adding text, can someone help and send me a script to
"Draw an ellipse shape that have the position and radius of the selected layer"
ran into problem at ellipseLayer.kind = LayerKind.ELLIPSE;

// Check if there is an active document

if (app.activeDocument) {

// Get the bounds of the selected layer

var selectedLayer = app.activeDocument.activeLayer;

var bounds = selectedLayer.bounds;

 

// Calculate the center and radius of the ellipse

var centerX = (bounds[0] + bounds[2]) / 2;

var centerY = (bounds[1] + bounds[3]) / 2;

var radiusX = (bounds[2] - bounds[0]) / 2;

var radiusY = (bounds[3] - bounds[1]) / 2;

 

// Create a new ellipse shape layer

var ellipseLayer = app.activeDocument.artLayers.add();

ellipseLayer.kind = LayerKind.ELLIPSE;

ellipseLayer.bounds = [centerX - radiusX, centerY - radiusY, centerX + radiusX, centerY + radiusY];

 

// Optional: You can customize the properties of the ellipse layer here

// For example, set the fill color

var fillColor = new SolidColor();

fillColor.rgb.red = 255;

fillColor.rgb.green = 0;

fill Color.rgb.blue = 0;

ellipseLayer.fillColor = fillColor;

 

// Optional: You can add more customization as needed

 

// Deselect the ellipse layer

app.activeDocument.selection.deselect();

} else {

alert("No active document found.");

}

TOPICS
Actions and scripting , Windows
342
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jan 14, 2024 Jan 14, 2024

You could clean this code up further, but the function »ellipseShapeLayer« itself should work fine. 

// 2024, use it at your own risk;
ellipseShapeLayer ([0,0,500,500], [0,128,255]);
//////
function ellipseShapeLayer (theBounds, theColor) {
// Make outer oval
var idPxl = charIDToTypeID( "#Pxl" );
var idClr = charIDToTypeID( "Clr " );
var idRd = charIDToTypeID( "Rd  " );
var idGrn = charIDToTypeID( "Grn " );
var idBl = charIDToTypeID( "Bl  " );
var idRGBC = charIDToTypeID( "RGBC" );
var idcontent
...
Translate
Community Expert , Jan 15, 2024 Jan 15, 2024

Good to hear! 

 

I cleaned up the function a little more and now it takes arguments for the Stroke Width and Color, too. (Stroke Width 0 meaning no Stroke.)

It should probably also include the option for no Fill and just a Stroke … but if you should ever need that maybe you can figure the necessary changes out yourself or post again. 

// 2024, use it at your own risk;
ellipseShapeLayer ([500,500,1000,1000], [0,128,255], 10, [0,0,0]);
//////
function ellipseShapeLayer (theBounds, theColor, stroke
...
Translate
Adobe
Community Expert ,
Jan 14, 2024 Jan 14, 2024

You could clean this code up further, but the function »ellipseShapeLayer« itself should work fine. 

// 2024, use it at your own risk;
ellipseShapeLayer ([0,0,500,500], [0,128,255]);
//////
function ellipseShapeLayer (theBounds, theColor) {
// Make outer oval
var idPxl = charIDToTypeID( "#Pxl" );
var idClr = charIDToTypeID( "Clr " );
var idRd = charIDToTypeID( "Rd  " );
var idGrn = charIDToTypeID( "Grn " );
var idBl = charIDToTypeID( "Bl  " );
var idRGBC = charIDToTypeID( "RGBC" );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
var idPnt = charIDToTypeID( "#Pnt" );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
var idMk = charIDToTypeID( "Mk  " );
    var desc41 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref12 = new ActionReference();
        ref12.putClass( idcontentLayer );
    desc41.putReference( idnull, ref12 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc42 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc43 = new ActionDescriptor();
                var desc44 = new ActionDescriptor();
                desc44.putDouble( idRd, theColor[0] );
                desc44.putDouble( idGrn, theColor[1] );
                desc44.putDouble( idBl, theColor[2] );
            desc43.putObject( idClr, idRGBC, desc44 );
        desc42.putObject( idType, idsolidColorLayer, desc43 );
            var desc45 = new ActionDescriptor();
            desc45.putUnitDouble( charIDToTypeID( "Top " ), idPxl, theBounds[0] );
            desc45.putUnitDouble( charIDToTypeID( "Left" ), idPxl, theBounds[1] );
            desc45.putUnitDouble( charIDToTypeID( "Btom" ), idPxl, theBounds[2] );
            desc45.putUnitDouble( charIDToTypeID( "Rght" ), idPxl, theBounds[3] );
        var idElps = charIDToTypeID( "Elps" );
        desc42.putObject( charIDToTypeID( "Shp " ), idElps, desc45 );
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            var desc46 = new ActionDescriptor();
            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
            desc46.putInteger( idstrokeStyleVersion, 2 );
            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
            desc46.putBoolean( idstrokeEnabled, false );
            var idfillEnabled = stringIDToTypeID( "fillEnabled" );
            desc46.putBoolean( idfillEnabled, true );
            var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
            desc46.putUnitDouble( idstrokeStyleLineWidth, idPnt, 8.000000 );
            var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" );
            desc46.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 );
            var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" );
            desc46.putDouble( idstrokeStyleMiterLimit, 100.000000 );
            var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
            var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" );
            desc46.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap );
            var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
            var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" );
            desc46.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin );
            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
            var idstrokeStyleAlignOutside = stringIDToTypeID( "strokeStyleAlignOutside" );
            desc46.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignOutside );
            var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" );
            desc46.putBoolean( idstrokeStyleScaleLock, false );
            var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" );
            desc46.putBoolean( idstrokeStyleStrokeAdjust, false );
            var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" );
                var list3 = new ActionList();
            desc46.putList( idstrokeStyleLineDashSet, list3 );
            var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" );
            var idBlnM = charIDToTypeID( "BlnM" );
            var idNrml = charIDToTypeID( "Nrml" );
            desc46.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml );
            var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" );
            var idPrc = charIDToTypeID( "#Prc" );
            desc46.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 );
                var desc47 = new ActionDescriptor();
                    var desc48 = new ActionDescriptor();
                    desc48.putDouble( idRd, 255.000000 );
                    desc48.putDouble( idGrn, 0.000000 );
                    desc48.putDouble( idBl, 0.000000 );
                desc47.putObject( idClr, idRGBC, desc48 );
            desc46.putObject( stringIDToTypeID( "strokeStyleContent" ), idsolidColorLayer, desc47 );
            desc46.putDouble( stringIDToTypeID( "strokeStyleResolution" ), 300 );
        desc42.putObject( idstrokeStyle, idstrokeStyle, desc46 );
    desc41.putObject( idUsng, idcontentLayer, desc42 );
executeAction( idMk, desc41, DialogModes.NO );
};

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 14, 2024 Jan 14, 2024

Thanks you the code work perfectly, but its a little too complex for me to understand, can you help me futher by mark down where to adjust size and position to an anchor layer?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 14, 2024 Jan 14, 2024

Actually nevermind, I got the gisp, THANK YOU SO MUCH ❤️

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2024 Jan 15, 2024
LATEST

Good to hear! 

 

I cleaned up the function a little more and now it takes arguments for the Stroke Width and Color, too. (Stroke Width 0 meaning no Stroke.)

It should probably also include the option for no Fill and just a Stroke … but if you should ever need that maybe you can figure the necessary changes out yourself or post again. 

// 2024, use it at your own risk;
ellipseShapeLayer ([500,500,1000,1000], [0,128,255], 10, [0,0,0]);
//////
function ellipseShapeLayer (theBounds, theColor, strokeWidth, strokeColor) {
try {
// Make outer oval
var idPxl = charIDToTypeID( "#Pxl" );
var idPnt = charIDToTypeID( "#Pnt" );
var idClr = charIDToTypeID( "Clr " );
var idRd = charIDToTypeID( "Rd  " );
var idGrn = charIDToTypeID( "Grn " );
var idBl = charIDToTypeID( "Bl  " );
var idRGBC = charIDToTypeID( "RGBC" );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
    var desc41 = new ActionDescriptor();
        var ref12 = new ActionReference();
        ref12.putClass( idcontentLayer );
    desc41.putReference( charIDToTypeID( "null" ), ref12 );
        var desc42 = new ActionDescriptor();
            var desc43 = new ActionDescriptor();
                var desc44 = new ActionDescriptor();
                desc44.putDouble( idRd, theColor[0] );
                desc44.putDouble( idGrn, theColor[1] );
                desc44.putDouble( idBl, theColor[2] );
            desc43.putObject( idClr, idRGBC, desc44 );
        desc42.putObject( charIDToTypeID( "Type" ), idsolidColorLayer, desc43 );
            var desc45 = new ActionDescriptor();
            desc45.putUnitDouble( charIDToTypeID( "Top " ), idPxl, theBounds[0] );
            desc45.putUnitDouble( charIDToTypeID( "Left" ), idPxl, theBounds[1] );
            desc45.putUnitDouble( charIDToTypeID( "Btom" ), idPxl, theBounds[2] );
            desc45.putUnitDouble( charIDToTypeID( "Rght" ), idPxl, theBounds[3] );
        desc42.putObject( charIDToTypeID( "Shp " ), charIDToTypeID( "Elps" ), desc45 );
            var desc46 = new ActionDescriptor();
            desc46.putInteger( stringIDToTypeID( "strokeStyleVersion" ), 2 );
            if (strokeWidth == 0) {desc46.putBoolean( stringIDToTypeID( "strokeEnabled" ), false )}
            else {desc46.putBoolean( stringIDToTypeID( "strokeEnabled" ), true )};
            desc46.putBoolean( stringIDToTypeID( "fillEnabled" ), true );
            desc46.putUnitDouble( stringIDToTypeID( "strokeStyleLineWidth" ), idPnt, strokeWidth );
            desc46.putUnitDouble( stringIDToTypeID( "strokeStyleLineDashOffset" ), idPnt, 0.000000 );
            desc46.putDouble( stringIDToTypeID( "strokeStyleMiterLimit" ), 100.000000 );
            desc46.putEnumerated( stringIDToTypeID( "strokeStyleLineCapType" ), stringIDToTypeID( "strokeStyleButtCap" ), stringIDToTypeID( "strokeStyleButtCap" ) );
            desc46.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, stringIDToTypeID( "strokeStyleMiterJoin" ) );
            desc46.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, stringIDToTypeID( "strokeStyleAlignCenter" ) );//strokeStyleAlignOutside
            desc46.putBoolean( stringIDToTypeID( "strokeStyleScaleLock" ), false );
            desc46.putBoolean( stringIDToTypeID( "strokeStyleStrokeAdjust" ), false );
                var list3 = new ActionList();
            desc46.putList( stringIDToTypeID( "strokeStyleLineDashSet" ), list3 );
            desc46.putEnumerated( stringIDToTypeID( "strokeStyleBlendMode" ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );
            desc46.putUnitDouble( stringIDToTypeID( "strokeStyleOpacity" ), charIDToTypeID( "#Prc" ), 100.000000 );
                var desc47 = new ActionDescriptor();
                    var desc48 = new ActionDescriptor();
                    desc48.putDouble( idRd, strokeColor[0] );
                    desc48.putDouble( idGrn, strokeColor[1] );
                    desc48.putDouble( idBl, strokeColor[2] );
                desc47.putObject( idClr, idRGBC, desc48 );
            desc46.putObject( stringIDToTypeID( "strokeStyleContent" ), idsolidColorLayer, desc47 );
            desc46.putDouble( stringIDToTypeID( "strokeStyleResolution" ), 300 );
        desc42.putObject( idstrokeStyle, idstrokeStyle, desc46 );
    desc41.putObject( charIDToTypeID( "Usng" ), idcontentLayer, desc42 );
executeAction( charIDToTypeID( "Mk  " ), desc41, DialogModes.NO );
} catch (e) {alert ("fail")}
};

Screenshot 2024-01-15 at 17.13.14.pngexpand imageScreenshot 2024-01-15 at 17.13.21.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines