Skip to main content
Inspiring
June 11, 2015
Answered

How can I draw a rectangle?

  • June 11, 2015
  • 3 replies
  • 3408 views

I need to simulate drawing a rectangle with the Rectangle tool in a script. One way is to use the Script Listener and modify the code so I can pass the necessary variables. Is there a better solution? Thanks.

This topic has been closed for replies.
Correct answer I have gone

You can draw a rectangle as follows.

#target photoshop;

app.bringToFront();

main();

function main(){

try{

app.displayDialogs = DialogModes.NO;

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

var Colour = new SolidColor;

Colour.rgb.hexValue = 'ff00ff';

app.documents.add();

activeDocument.selection.select([[10, 10], [494, 10], [494, 350], [10,350]], SelectionType.REPLACE, 0, false);

activeDocument.selection.stroke (Colour, 4, StrokeLocation.INSIDE,ColorBlendMode.NORMAL,100);

activeDocument.selection.deselect();

}catch(e){}

finally{

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

};

};

3 replies

c.pfaffenbichler
Community Expert
Community Expert
June 12, 2015

Limited support for Shapes is one clear shortcoming of the Javascript API.

Can that not all be done with AM code?

Or did you mean the Photoshop DOM when you wrote "Javascript API"?

matias.kiviniemi
Legend
June 12, 2015

Yeah, I meant DOM.

c.pfaffenbichler
Community Expert
Community Expert
June 12, 2015

No denying DOM code is "easier on the eye" but where it really gets frustrating is where properties/features are even off-limits to AM code …

DBarranca
Legend
June 11, 2015

Hi Bazsl‌,

I'm afraid there is no DOM way to access the Rectangle tool so Scripting Listener is the way, as far as I know.

(this if you're asking about the vector rectangle tool, otherwise Philip's solution works just fine)

Regards,

Davide Barranca

---

www.davidebarranca.com

www.cs-extensions.com

matias.kiviniemi
Legend
June 12, 2015

Limited support for Shapes is one clear shortcoming of the Javascript API.. Reading & manipulating fill, stroke, etc. settings would be very nice.

I have goneCorrect answer
Inspiring
June 11, 2015

You can draw a rectangle as follows.

#target photoshop;

app.bringToFront();

main();

function main(){

try{

app.displayDialogs = DialogModes.NO;

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

var Colour = new SolidColor;

Colour.rgb.hexValue = 'ff00ff';

app.documents.add();

activeDocument.selection.select([[10, 10], [494, 10], [494, 350], [10,350]], SelectionType.REPLACE, 0, false);

activeDocument.selection.stroke (Colour, 4, StrokeLocation.INSIDE,ColorBlendMode.NORMAL,100);

activeDocument.selection.deselect();

}catch(e){}

finally{

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

};

};