public
Last active

Draw Shape in Photoshop with JavaScript

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* 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]);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.