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

any Selection Shape area to rectangular selection shape photoshop Scripts

Explorer ,
Oct 01, 2023 Oct 01, 2023

Copy link to clipboard

Copied

Please Can You Help Me ?

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

TOPICS
Windows

Views

390

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

Community Expert , Oct 01, 2023 Oct 01, 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
...

Votes

Translate

Translate
Adobe
Explorer ,
Oct 01, 2023 Oct 01, 2023

Copy link to clipboard

Copied

01.jpg

Votes

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 Expert ,
Oct 01, 2023 Oct 01, 2023

Copy link to clipboard

Copied

Courtesy of @c.pfaffenbichler 

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-automatically-create-a-rect...

 

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

 

Votes

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 Expert ,
Oct 01, 2023 Oct 01, 2023

Copy link to clipboard

Copied

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

 

Votes

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 Expert ,
Oct 01, 2023 Oct 01, 2023

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.

Votes

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 Expert ,
Oct 01, 2023 Oct 01, 2023

Copy link to clipboard

Copied

@Bojan Živković – Hmm, curious, the following screenshot is before (left) and after (right) in v2024:

 

2023-10-02_17-24-05 (1).png

 

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

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

script to rectangular.gif

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

YMMV :]

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

quote

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

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

Unfortunately no, changing Ruler to Pixels is not solution on my side.

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

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

Votes

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 Expert ,
Oct 03, 2023 Oct 03, 2023

Copy link to clipboard

Copied

LATEST
quote

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! :]

Votes

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 Expert ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

quote
quote

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.

Votes

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
Explorer ,
Oct 02, 2023 Oct 02, 2023

Copy link to clipboard

Copied

Thank you so much Sir @Stephen_A_Marsh 

This Scripts is Working on my window OS

 

Votes

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