• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Vector rectangle shape made with ExtendScript?

Community Beginner ,
Feb 25, 2019 Feb 25, 2019

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

TOPICS
Actions and scripting

Views

1.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Feb 26, 2019 Feb 26, 2019

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

...

Likes

Translate

Translate
Valorous Hero ,
Feb 26, 2019 Feb 26, 2019

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);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

Hi r-bin,

Thank you so much for your code, this is perfect

fred

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 28, 2019 Mar 28, 2019

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

  • 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);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2019 Mar 28, 2019

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.

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 28, 2019 Mar 28, 2019

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...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2019 Mar 28, 2019

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

Capture.jpg

You can not add a vector laye mask to a shape layer for it has one soe vector mask will be grayed out

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 28, 2019 Mar 28, 2019

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); 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 29, 2019 Mar 29, 2019

Copy link to clipboard

Copied

Hey r-bin,

Thank you so much, it worked perfectly!!!

Cheers,

Fred

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 18, 2022 Sep 18, 2022

Copy link to clipboard

Copied

LATEST

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines