Copy link to clipboard
Copied
Please Can You Help Me ?
Change from any selection shape area to rectangular selection shape photoshop Scripts
Courtesy of @c.pfaffenbichler
/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-automatically-create-a-rectangular-layer-mask-from-an-irregularly-shaped-selection/m-p/4905764 */
// make selection rectangular
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var
...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Courtesy of @c.pfaffenbichler
/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-automatically-create-a-rectangular-layer-mask-from-an-irregularly-shaped-selection/m-p/4905764 */
// make selection rectangular
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theBounds = myDocument.selection.bounds;
var theArray = [[theBounds[0], theBounds[1]], [theBounds[2], theBounds[1]], [theBounds[2], theBounds[3]], [theBounds[0], theBounds[3]]];
myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);
}
catch (e) { }
}
Copy link to clipboard
Copied
For some reason, the previous code isn't returning a rectangular selection, so I quickly hacked this together:
/*
Selection to Rectangular Selection.jsx
v1.0 - 2nd October 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/any-selection-shape-area-to-rectangular-selection-shape-photoshop-scripts/td-p/14125723
*/
try {
// Test for an active selection
app.activeDocument.selection.bounds;
// Get and set the ruler units
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Generic selection bounds variables
var selectionBounds = activeDocument.selection.bounds;
var selectionLeft = selectionBounds[0].value;
var selectionTop = selectionBounds[1].value;
var selectionRight = selectionBounds[2].value;
var selectionBottom = selectionBounds[3].value;
var selectionWidth = selectionBounds[2].value - selectionBounds[0].value;
var selectionHeight = selectionBounds[3].value - selectionBounds[1].value;
var selectionXCenter = (selectionBounds[0].value + selectionWidth) / 2;
var selectionYCenter = (selectionBounds[1].value + selectionHeight) / 2;
// Selection bounds to rectangular selection
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putProperty(s2t("channel"), s2t("selection"));
descriptor.putReference(s2t("null"), reference);
descriptor2.putUnitDouble(s2t("top"), s2t("distanceUnit"), selectionTop);
descriptor2.putUnitDouble(s2t("left"), s2t("distanceUnit"), selectionLeft);
descriptor2.putUnitDouble(s2t("bottom"), s2t("distanceUnit"), selectionBottom);
descriptor2.putUnitDouble(s2t("right"), s2t("distanceUnit"), selectionRight);
descriptor.putObject(s2t("to"), s2t("rectangle"), descriptor2);
executeAction(s2t("set"), descriptor, DialogModes.NO);
// Restore the original ruler units
app.preferences.rulerUnits = origRulerUnits;
} catch (e) {
alert("There is no active selection!");
}
Copy link to clipboard
Copied
I am testing on Windows, running Photoshop 25.0.0. Both scripts do not produce the requested result.
The first script is creating a small rectangle in the top-left corner, while the second is creating a huge rectangle off the canvas. Only one part of the selection rectangle is visible in the top-right corner of the image.
Copy link to clipboard
Copied
@Bojan Živković – Hmm, curious, the following screenshot is before (left) and after (right) in v2024:
Is your ruler origin point in the upper left? The guides in the screenshot are only to aid visualisation.
EDIT: Also tested in v2021 with expected results (Mac).
Copy link to clipboard
Copied
Copy link to clipboard
Copied
YMMV :]
Copy link to clipboard
Copied
I am testing on Windows, running Photoshop 25.0.0. Both scripts do not produce the requested result.
The first script is creating a small rectangle in the top-left corner, while the second is creating a huge rectangle off the canvas. Only one part of the selection rectangle is visible in the top-right corner of the image.
By @Bojan Živković
Sound like it might be related the the image resolution.
Changing the ruler settings to Pixels should help there. (Which could be done via Script and reset via Script, too.)
Copy link to clipboard
Copied
Unfortunately no, changing Ruler to Pixels is not solution on my side.
Copy link to clipboard
Copied
Update: @c.pfaffenbichler script actually works as exopected after changing Ruler to Pixels, I was testing second script by @Stephen_A_Marsh all the time after initial test.
// make selection rectangular
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theBounds = myDocument.selection.bounds;
var theArray = [[theBounds[0], theBounds[1]], [theBounds[2], theBounds[1]], [theBounds[2], theBounds[3]], [theBounds[0], theBounds[3]]];
myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);
}
catch (e) { }
}
Copy link to clipboard
Copied
Update: @c.pfaffenbichler script actually works as exopected after changing Ruler to Pixels, I was testing second script by @Stephen_A_Marsh all the time after initial test.
By @Bojan Živković
That sounds about right, the student has far to go! :]
Copy link to clipboard
Copied
I am testing on Windows, running Photoshop 25.0.0. Both scripts do not produce the requested result.
The first script is creating a small rectangle in the top-left corner, while the second is creating a huge rectangle off the canvas. Only one part of the selection rectangle is visible in the top-right corner of the image.
By @Bojan ŽivkovićSound like it might be related the the image resolution.
Changing the ruler settings to Pixels should help there. (Which could be done via Script and reset via Script, too.)
By @c.pfaffenbichler
That's the strange thing, my code does set the units to px and also resets the ruler units to the original.
Selections can be complicated, my script is only intended for a simple, single isolated selection floating in the centre of the image as per the original request.
Copy link to clipboard
Copied