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

Transform Selection Script

Contributor ,
Jan 26, 2022 Jan 26, 2022

Hi,

Im looking for a way to make a script that tranform the actual selection but like the way i can do in Photoshop menubar.

like Transform 100%+2px?

 

that would help me so much if anyone had an idea.

 

best Regards

TOPICS
Actions and scripting
1.6K
Translate
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

LEGEND , Jan 26, 2022 Jan 26, 2022

 

preferences.rulerUnits = Units.PIXELS
b = (slctn = (aD = activeDocument).selection).bounds
p = 2 / ((b[2] - b[0] + (b[3] - b[1])) / 200)
slctn.resizeBoundary(100 + p, 100 + p)

 

Translate
Adobe
LEGEND ,
Jan 26, 2022 Jan 26, 2022

2 pixels in relation to distance of which object?

Translate
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 ,
Jan 26, 2022 Jan 26, 2022

I read this as extending the selection bounds, what else could it be?

Translate
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 ,
Jan 26, 2022 Jan 26, 2022

I thought of the same, but as it needs a little more calculation than taking it from document dimension I wanted to be sure. Too many time users ask for something else than they write.

Translate
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
Contributor ,
Jan 26, 2022 Jan 26, 2022

Hi there,
I mean that if I have an existing selection, Transform it. In Photoshop itself, the transformation is actually given in % values, but if I enter 100%+2px, then the selection is enlarged by exactly 2 pixels. This has nothing to do with enlarge selection because here the pixels are expanded in every direction.

Transform Selection.jpg

Translate
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 ,
Jan 26, 2022 Jan 26, 2022

 

preferences.rulerUnits = Units.PIXELS
b = (slctn = (aD = activeDocument).selection).bounds
p = 2 / ((b[2] - b[0] + (b[3] - b[1])) / 200)
slctn.resizeBoundary(100 + p, 100 + p)

 

Translate
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
Contributor ,
Jan 26, 2022 Jan 26, 2022

Thank you very much! that helps me a lot 🙂

Translate
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 ,
Jan 26, 2022 Jan 26, 2022

Is this to be a fixed value hard coded, or does it come from somewhere else or perhaps a user input interface prompt?

Translate
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 ,
Jan 27, 2022 Jan 27, 2022

@Kukurykus has as always provided a concise, minimilist code offering.

 

I had a number of false starts and dead ends, however, I finally came up with the following code, which is by no surprise more verbose and clunky... But I had fun anyway! 🙂

 

P.S. Forgot to add, this only works for a rectangular selection.

 

/*
Resize rectangular selection from centre by relative px values.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/transform-selection-script/td-p/12707931
Stephen Marsh, v1.0 - 27th January 2022
NOTE: Recreates the active selection as a rectangular selection, so will not work for elliptical or freeform selections
*/

//    Bounds:
//    + - - - - - - - [1] - - - - - - - +
//    |                                 |
//    |                                 |
//   [0]                               [2]
//    |                                 |
//    |                                 |
//    + - - - - - - - [3] - - - - - - - +

// Save the current ruler units and set to pixels
var origRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Relative resize values in px
var addWidth = 2;
var addHeight = 2;

// Selection bounds
var theBounds = app.activeDocument.selection.bounds;

// Selection array (top left, top right, bottom right, bottom left)
var theArray = [
    [theBounds[0], theBounds[1]],
    [theBounds[2] + addWidth, theBounds[1]],
    [theBounds[2] + addWidth, theBounds[3] + addHeight],
    [theBounds[0], theBounds[3] + addHeight]
];

// Replace the selection with new selection
app.activeDocument.selection.select( theArray, SelectionType.REPLACE, 0, false );

// Centre the selection
app.activeDocument.selection.translateBoundary( -addWidth / 2, -addHeight / 2 );

// Restore the ruler units
app.preferences.rulerUnits = origRulers;

 

Translate
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 ,
Jan 27, 2022 Jan 27, 2022

This version works with elliptical marquee selections.

 

/*
Resize elliptical selection from centre by relative px values.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/transform-selection-script/td-p/12707931
Stephen Marsh, v1.0 - 27th January 2022
NOTE: Recreates the active selection as an elliptical selection, so will not work for rectangular or freeform selections
*/

//    Bounds:
//    + - - - - - - - [1] - - - - - - - +
//    |                                 |
//    |                                 |
//   [0]                               [2]
//    |                                 |
//    |                                 |
//    + - - - - - - - [3] - - - - - - - +

// Save the current ruler units and set to pixels
var origRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Relative resize values in px
var addWidth = 2;
var addHeight = 2;

// Selection bounds
var theBounds = app.activeDocument.selection.bounds;

// Bounds variables
var selTop = theBounds[1].value;
var selLeft = theBounds[0].value;
var selRight = theBounds[2].value;
var selBottom = theBounds[3].value;

// Create the elliptical selection
ellipticalSelection( selTop, selLeft, selBottom, selRight, true );

// Centre the selection
app.activeDocument.selection.translateBoundary( -addWidth / 2, -addHeight / 2 );

// Restore the ruler units
app.preferences.rulerUnits = origRulers;


// Functions

function ellipticalSelection(top, left, bottom, right, antiAlias) {
	var s2t = function (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( "pixelsUnit" ), top );
	descriptor2.putUnitDouble( s2t( "left" ), s2t( "pixelsUnit" ), left );
	descriptor2.putUnitDouble( s2t( "bottom" ), s2t( "pixelsUnit" ), bottom + addHeight);
	descriptor2.putUnitDouble( s2t( "right" ), s2t( "pixelsUnit" ), right + addWidth);
	descriptor.putObject( s2t( "to" ), s2t( "ellipse" ), descriptor2 );
	descriptor.putBoolean( s2t( "antiAlias" ), antiAlias );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

 

Translate
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 ,
Jan 27, 2022 Jan 27, 2022

Mine too.

Translate
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 ,
Jan 27, 2022 Jan 27, 2022

@Kukurykus wrote:

Mine too.


 

Of course your code works with both rectangular and elliptical in one script, it was well planned! 🙂

 

I was so desperate to avoid resizing by % that I thought that using bounding co-ordinates would be better, which led me to creating a new selection rather than resizing the existing selection.

 

I would have just gone with a single line .expand() if I was after something easier that is "close enough but not perfect" for an elliptical/circular selection.

Translate
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 ,
Jan 27, 2022 Jan 27, 2022
LATEST

To imitate transforming the expanding has sense only when there are no holes in selection.

Translate
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