Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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]);
@rostfirsikov

This comment has been minimized.

Copy link

@rostfirsikov rostfirsikov commented Oct 9, 2019

I'm sorry. I am new to GitHub and don't know how to ask right here.

I can’t understand which units of measure use this script. For example, I am creating a new document in Photoshop with a width and height of 400 pixels and a resolution of 300 ppi. Then I run your script with just one function call:

DrawShape ([100, 100], [100, 200], [200, 200], [200, 100]);

In theory, I should get a square right in the center of the document. But as a result, I get a square shape with equal coordinates and a size of 415.69 pixels or 35.30 mm.

How can i fix this?

@vladocar

This comment has been minimized.

Copy link
Owner Author

@vladocar vladocar commented Oct 9, 2019

I run some tests, the problem is 300 ppi.. it will only work on 72 ppi.

@rostfirsikov

This comment has been minimized.

Copy link

@rostfirsikov rostfirsikov commented Oct 9, 2019

Thanx for answer. Can you give me some advice how to create shapes in documents with any resolution?

@rostfirsikov

This comment has been minimized.

Copy link

@rostfirsikov rostfirsikov commented Oct 10, 2019

My solution is

var docCoef = Doc.resolution / 72;
DrawShape([100/docCoef, 100/docCoef], [100/docCoef, 200/docCoef], [200/docCoef, 200/docCoef], [200/docCoef, 100/docCoef]);

But it's too dirty

@vladocar

This comment has been minimized.

Copy link
Owner Author

@vladocar vladocar commented Oct 10, 2019

There must be some better solution to be used in the script directly.. but if it works it's Ok.

@rostfirsikov

This comment has been minimized.

Copy link

@rostfirsikov rostfirsikov commented Oct 10, 2019

I'm not a programmer. I'm a DTP artist. I don't know JavaScript as a programmer.

@psCharts

This comment has been minimized.

Copy link

@psCharts psCharts commented Oct 15, 2019

When the number of points is more than 1000, they will not work. Can you solve it?

@michaelprovenzano

This comment has been minimized.

Copy link

@michaelprovenzano michaelprovenzano commented Nov 13, 2019

I modified the script.

  • Now it will do all the conversions necessary to work at whatever document resolution you are in. (It does only accept parameters in pixels.)
  • The first parameter must be an RGB color
  • You can add additional points in a point to make it a bezier point. See commented examples in the script
@vladocar

This comment has been minimized.

Copy link
Owner Author

@vladocar vladocar commented Nov 13, 2019

Thanks!

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