Copy link to clipboard
Copied
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
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)
Copy link to clipboard
Copied
2 pixels in relation to distance of which object?
Copy link to clipboard
Copied
I read this as extending the selection bounds, what else could it be?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Thank you very much! that helps me a lot 🙂
Copy link to clipboard
Copied
Is this to be a fixed value hard coded, or does it come from somewhere else or perhaps a user input interface prompt?
Copy link to clipboard
Copied
@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;
Copy link to clipboard
Copied
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 );
}
Copy link to clipboard
Copied
Mine too.
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
To imitate transforming the expanding has sense only when there are no holes in selection.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more