Skip to content

Instantly share code, notes, and snippets.

@vladocar
Created October 23, 2011 22:33
Show Gist options
  • Save vladocar/1307987 to your computer and use it in GitHub Desktop.
Save vladocar/1307987 to your computer and use it in GitHub Desktop.
Draw Shape in Photoshop with JavaScript
/* Code by Mike Hale http://www.ps-scripts.com/bb/viewtopic.php?f=14&t=1802&start=15
with small modification by Vladimir Carrer
*/
function DrawShape() {
var doc = app.activeDocument;
var y = arguments.length;
var i = 0;
var lineArray = [];
for (i = 0; i < y; i++) {
lineArray[i] = new PathPointInfo;
lineArray[i].kind = PointKind.CORNERPOINT;
lineArray[i].anchor = arguments[i];
lineArray[i].leftDirection = lineArray[i].anchor;
lineArray[i].rightDirection = lineArray[i].anchor;
}
var lineSubPathArray = new SubPathInfo();
lineSubPathArray.closed = true;
lineSubPathArray.operation = ShapeOperation.SHAPEADD;
lineSubPathArray.entireSubPath = lineArray;
var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]);
var desc88 = new ActionDescriptor();
var ref60 = new ActionReference();
ref60.putClass(stringIDToTypeID("contentLayer"));
desc88.putReference(charIDToTypeID("null"), ref60);
var desc89 = new ActionDescriptor();
var desc90 = new ActionDescriptor();
var desc91 = new ActionDescriptor();
desc91.putDouble(charIDToTypeID("Rd "), 0.000000); // R
desc91.putDouble(charIDToTypeID("Grn "), 0.000000); // G
desc91.putDouble(charIDToTypeID("Bl "), 0.000000); // B
var id481 = charIDToTypeID("RGBC");
desc90.putObject(charIDToTypeID("Clr "), id481, desc91);
desc89.putObject(charIDToTypeID("Type"), stringIDToTypeID("solidColorLayer"), desc90);
desc88.putObject(charIDToTypeID("Usng"), stringIDToTypeID("contentLayer"), desc89);
executeAction(charIDToTypeID("Mk "), desc88, DialogModes.NO);
myPathItem.remove();
}
// X,Y
// Put the coordinates in clockwise order
DrawShape([100, 100], [100, 200], [200, 200], [200, 100]);
DrawShape([512, 128], [600, 256], [684, 320], [600, 386], [686, 514], [512, 450],[340,512],[428,386],[340,320],[428,256]);
@Fitspade
Copy link

Fitspade commented Jun 16, 2022

Hi, is there any other way to draw a shape without using PathPointInfo ? Because I want to draw it in all units of the ruler but when I convert the position (pixels to points for example), I get an error: "Error point value expected".
When I draw a circle or a rectangle, I have no problem to convert but I go directly through the code retrieved by a macro with the Script Listener plugin.

@michaelprovenzano
Copy link

michaelprovenzano commented Jun 21, 2022

I don't know of another way to draw a shape, however, I did create a fork of this gist and added the ability to set the color of the shape and the units you are using to define your shape. Calling the function is a bit different, but it is more flexible out of the box.

https://gist.github.com/michaelprovenzano/da9c834e25c2ff1f7b1ccc3cbad57c82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment