Skip to main content
Known Participant
October 1, 2023
Answered

any Selection Shape area to rectangular selection shape photoshop Scripts

  • October 1, 2023
  • 1 reply
  • 894 views

Please Can You Help Me ?

Change from any selection shape area to rectangular selection shape photoshop Scripts 

This topic has been closed for replies.
Correct answer Stephen Marsh

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

 

/* 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) { }
}

 

1 reply

ThihasoeAuthor
Known Participant
October 1, 2023

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 2, 2023

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

 

/* 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) { }
}

 

Stephen Marsh
Community Expert
Community Expert
October 2, 2023

@Thihasoe 

 

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!");
}