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]);
@rostfirsikov
Copy link

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
Copy link
Author

vladocar commented Oct 9, 2019

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

@rostfirsikov
Copy link

rostfirsikov commented Oct 9, 2019

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

@rostfirsikov
Copy link

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
Copy link
Author

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

@rostfirsikov
Copy link

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

@psCharts
Copy link

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

@michaelprovenzano
Copy link

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
Copy link
Author

Thanks!

@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