Copy link to clipboard
Copied
Hello,
Is it possible to create a vector rectangle shape (rectangle tool) with ExtendScript?
I just want to create a 1px border around my PSD file and keep it vector in order to change the width later (to 2px for example).
Thanks for your help,
fred
Maybe this is what you need.
...var w = 1; // px
var tmp = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var x = activeDocument.width.value;
var y = activeDocument.height.value;
app.preferences.rulerUnits = tmp;
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("contentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), new ActionDescr
Copy link to clipboard
Copied
Maybe this is what you need.
var w = 1; // px
var tmp = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var x = activeDocument.width.value;
var y = activeDocument.height.value;
app.preferences.rulerUnits = tmp;
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("contentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), new ActionDescriptor());
var d2 = new ActionDescriptor();
d2.putUnitDouble(stringIDToTypeID("top"), stringIDToTypeID("pixelsUnit"), 0);
d2.putUnitDouble(stringIDToTypeID("left"), stringIDToTypeID("pixelsUnit"), 0);
d2.putUnitDouble(stringIDToTypeID("bottom"), stringIDToTypeID("pixelsUnit"), y);
d2.putUnitDouble(stringIDToTypeID("right"), stringIDToTypeID("pixelsUnit"), x);
d1.putObject(stringIDToTypeID("shape"), stringIDToTypeID("rectangle"), d2);
var d2 = new ActionDescriptor();
d2.putBoolean(stringIDToTypeID("strokeEnabled"), true);
d2.putBoolean(stringIDToTypeID("fillEnabled"), false);
d2.putUnitDouble(stringIDToTypeID("strokeStyleLineWidth"), stringIDToTypeID("pixelsUnit"), w);
var d3 = new ActionDescriptor();
d3.putDouble(stringIDToTypeID("red"), 0);
d3.putDouble(stringIDToTypeID("green"), 0);
d3.putDouble(stringIDToTypeID("blue"), 0);
var d4 = new ActionDescriptor();
d4.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d3);
d2.putObject(stringIDToTypeID("strokeStyleContent"), stringIDToTypeID("solidColorLayer"), d4);
d1.putObject(stringIDToTypeID("strokeStyle"), stringIDToTypeID("strokeStyle"), d2);
d.putObject(stringIDToTypeID("using"), stringIDToTypeID("contentLayer"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
Copy link to clipboard
Copied
Hi r-bin,
Thank you so much for your code, this is perfect
fred
Copy link to clipboard
Copied
Hello again...
I would like to have a background color within this script... how can I do that?
I tried to set the following to true, it works but I have a default white bgd and I want to change the color.
d2.putBoolean(stringIDToTypeID("fillEnabled"), true);
Thank you,
fred
Copy link to clipboard
Copied
You would been to add a layer below the Shape layer and fill the layer with the color you want.
Copy link to clipboard
Copied
Thanks for your answer, but I would prefer to keep the Live Shape Properties of the Rectangle Tool... and use the Fill Color like it is already with the stroke color...
Copy link to clipboard
Copied
The shape layer will not change it has no background color the layer only has pixel within the shape's vector layer mask, Basicly a shape layer is some kind of fill layer shaped by a vector path. In old version of Photoshop the palled had thumb nails for shape layer vector layer mask. At some point in time Adobe stop displaying that vector mask thumbnail and instead represent the a layer was a shape layer with the shape icon in the Layer Content thumbnail. It is still possible to add visible vector layer mask. Its name is still color fill 1.
However if to add a vector layer mask to a fill layer Photoshop will not display the vector layer mask thumbnail in the layers palette Photoshop will display the shape in the layer content and stamp it with the shape icon. The layer will not longer be shown as a solid fill layer. The layer is still a fill layer its vector layer mask does exist and you can edit it. The layer is just depicted differently
You can not add a vector laye mask to a shape layer for it has one soe vector mask will be grayed out
Copy link to clipboard
Copied
In addition, it is necessary also to change
var d1 = new ActionDescriptor();
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), new ActionDescriptor());
to
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
var d3 = new ActionDescriptor();
d3.putDouble(stringIDToTypeID("red"), 255);
d3.putDouble(stringIDToTypeID("green"),128);
d3.putDouble(stringIDToTypeID("blue"), 111);
d2.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d3);
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), d2);
Copy link to clipboard
Copied
Copy link to clipboard
Copied
For anyone looking at the code in this discussion thinking that it looks like magic... Well, I was with you there.
I HIGHLY suggest installing the ScriptListener plugin:
https://helpx.adobe.com/photoshop/kb/downloadable-plugins-and-content.html
IF you are on an M1 mac (like I am), run Photoshop using Rosetta (the plugin does not run natively on m1 chips yet).
You might also need to notarize the plugin. After putting the plugin in the Plugins folder, open terminal and run below:
sudo xattr -dr com.apple.quarantine /Applications/Adobe\ Photoshop\ 2022/Plug-ins/ScriptingListener.plugin
Once you have this plugin working... Trust me, you will be glad you did this. It will accelerate the development of ExtendScript's by orders of magnitude. Look at the desktop and it will show you the actions Photoshop executed and you can port this over into your jsx.