Skip to main content
Innotex Nico Tanne
Inspiring
January 26, 2022
Answered

Transform Selection Script

  • January 26, 2022
  • 3 replies
  • 1495 views

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

This topic has been closed for replies.
Correct answer Kukurykus

 

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)

 

3 replies

Stephen Marsh
Community Expert
Community Expert
January 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;

 

Stephen Marsh
Community Expert
Community Expert
January 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 );
}

 

Kukurykus
Legend
January 27, 2022

Mine too.

Stephen Marsh
Community Expert
Community Expert
January 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?

Kukurykus
Legend
January 26, 2022

2 pixels in relation to distance of which object?

Stephen Marsh
Community Expert
Community Expert
January 26, 2022

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

Kukurykus
Legend
January 27, 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.