Skip to main content
Inspiring
January 9, 2023
Answered

Need help with a script to Free Transform a selection (PS 2023)

  • January 9, 2023
  • 2 replies
  • 466 views

Is it possible to move and rotate a selection using a script?

If so, I need some hints/help to get started.

Is there already a script that does this ?

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

@mangurian – the following code provides an interactive transform of a selection:

 

try {
    if (activeDocument.selection.bounds) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        descriptor.putEnumerated(s2t("interfaceIconFrameDimmed"), s2t("interpolationType"), s2t("bicubic"));
        executeAction(s2t("transform"), descriptor, DialogModes.ALL);
    }
} catch (e) {}

 

This is the raw code recorded by the SL plug-in, commented to show the values used in the original recording:

 

var idtransform = stringIDToTypeID( "transform" );
    var desc221 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
        var ref17 = new ActionReference();
        var idchannel = stringIDToTypeID( "channel" );
        var idselection = stringIDToTypeID( "selection" );
        ref17.putProperty( idchannel, idselection );
    desc221.putReference( idnull, ref17 );
    var idfreeTransformCenterState = stringIDToTypeID( "freeTransformCenterState" );
    var idquadCenterState = stringIDToTypeID( "quadCenterState" );
    var idQCSAverage = stringIDToTypeID( "QCSAverage" );
    desc221.putEnumerated( idfreeTransformCenterState, idquadCenterState, idQCSAverage );
    var idoffset = stringIDToTypeID( "offset" );
        var desc222 = new ActionDescriptor();
        var idhorizontal = stringIDToTypeID( "horizontal" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc222.putUnitDouble( idhorizontal, idpixelsUnit, -10 ); // -10px horizontal transform
        var idvertical = stringIDToTypeID( "vertical" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc222.putUnitDouble( idvertical, idpixelsUnit, -10 ); // -10px vertical transform
    var idoffset = stringIDToTypeID( "offset" );
    desc221.putObject( idoffset, idoffset, desc222 );
    var idwidth = stringIDToTypeID( "width" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc221.putUnitDouble( idwidth, idpercentUnit, 125.000000 ); // 125% width scale
    var idheight = stringIDToTypeID( "height" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc221.putUnitDouble( idheight, idpercentUnit, 125.000000 ); // 125% height scale
    var idskew = stringIDToTypeID( "skew" );
        var desc223 = new ActionDescriptor();
        var idhorizontal = stringIDToTypeID( "horizontal" );
        var idangleUnit = stringIDToTypeID( "angleUnit" );
        desc223.putUnitDouble( idhorizontal, idangleUnit, 10.000000 ); // 10 degree horizontal skew
        var idvertical = stringIDToTypeID( "vertical" );
        var idangleUnit = stringIDToTypeID( "angleUnit" );
        desc223.putUnitDouble( idvertical, idangleUnit, 10.000000 ); // 10 degree vertical skew
    var idpaint = stringIDToTypeID( "paint" );
    desc221.putObject( idskew, idpaint, desc223 );
    var idangle = stringIDToTypeID( "angle" );
    var idangleUnit = stringIDToTypeID( "angleUnit" );
    desc221.putUnitDouble( idangle, idangleUnit, 45.000000 ); // 45 degree Rotation angle
    var idlinked = stringIDToTypeID( "linked" );
    desc221.putBoolean( idlinked, true );
    var idinterfaceIconFrameDimmed = stringIDToTypeID( "interfaceIconFrameDimmed" );
    var idinterpolationType = stringIDToTypeID( "interpolationType" );
    var idbicubic = stringIDToTypeID( "bicubic" );
    desc221.putEnumerated( idinterfaceIconFrameDimmed, idinterpolationType, idbicubic );
executeAction( idtransform, desc221, DialogModes.NO );

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 9, 2023

@mangurian – the following code provides an interactive transform of a selection:

 

try {
    if (activeDocument.selection.bounds) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        descriptor.putEnumerated(s2t("interfaceIconFrameDimmed"), s2t("interpolationType"), s2t("bicubic"));
        executeAction(s2t("transform"), descriptor, DialogModes.ALL);
    }
} catch (e) {}

 

This is the raw code recorded by the SL plug-in, commented to show the values used in the original recording:

 

var idtransform = stringIDToTypeID( "transform" );
    var desc221 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
        var ref17 = new ActionReference();
        var idchannel = stringIDToTypeID( "channel" );
        var idselection = stringIDToTypeID( "selection" );
        ref17.putProperty( idchannel, idselection );
    desc221.putReference( idnull, ref17 );
    var idfreeTransformCenterState = stringIDToTypeID( "freeTransformCenterState" );
    var idquadCenterState = stringIDToTypeID( "quadCenterState" );
    var idQCSAverage = stringIDToTypeID( "QCSAverage" );
    desc221.putEnumerated( idfreeTransformCenterState, idquadCenterState, idQCSAverage );
    var idoffset = stringIDToTypeID( "offset" );
        var desc222 = new ActionDescriptor();
        var idhorizontal = stringIDToTypeID( "horizontal" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc222.putUnitDouble( idhorizontal, idpixelsUnit, -10 ); // -10px horizontal transform
        var idvertical = stringIDToTypeID( "vertical" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc222.putUnitDouble( idvertical, idpixelsUnit, -10 ); // -10px vertical transform
    var idoffset = stringIDToTypeID( "offset" );
    desc221.putObject( idoffset, idoffset, desc222 );
    var idwidth = stringIDToTypeID( "width" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc221.putUnitDouble( idwidth, idpercentUnit, 125.000000 ); // 125% width scale
    var idheight = stringIDToTypeID( "height" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc221.putUnitDouble( idheight, idpercentUnit, 125.000000 ); // 125% height scale
    var idskew = stringIDToTypeID( "skew" );
        var desc223 = new ActionDescriptor();
        var idhorizontal = stringIDToTypeID( "horizontal" );
        var idangleUnit = stringIDToTypeID( "angleUnit" );
        desc223.putUnitDouble( idhorizontal, idangleUnit, 10.000000 ); // 10 degree horizontal skew
        var idvertical = stringIDToTypeID( "vertical" );
        var idangleUnit = stringIDToTypeID( "angleUnit" );
        desc223.putUnitDouble( idvertical, idangleUnit, 10.000000 ); // 10 degree vertical skew
    var idpaint = stringIDToTypeID( "paint" );
    desc221.putObject( idskew, idpaint, desc223 );
    var idangle = stringIDToTypeID( "angle" );
    var idangleUnit = stringIDToTypeID( "angleUnit" );
    desc221.putUnitDouble( idangle, idangleUnit, 45.000000 ); // 45 degree Rotation angle
    var idlinked = stringIDToTypeID( "linked" );
    desc221.putBoolean( idlinked, true );
    var idinterfaceIconFrameDimmed = stringIDToTypeID( "interfaceIconFrameDimmed" );
    var idinterpolationType = stringIDToTypeID( "interpolationType" );
    var idbicubic = stringIDToTypeID( "bicubic" );
    desc221.putEnumerated( idinterfaceIconFrameDimmed, idinterpolationType, idbicubic );
executeAction( idtransform, desc221, DialogModes.NO );

 

Stephen Marsh
Community Expert
Community Expert
January 11, 2023

@mangurian – Thank you for taking the time to mark my answer as correct. What were you looking for, the interactive selection transform or the fixed-value one?

Stephen Marsh
Community Expert
Community Expert
January 9, 2023
quote

Is it possible to move and rotate a selection using a script?

By @mangurian

 

Yes!

 

quote

If so, I need some hints/help to get started.

 

Use the ScriptingListener plug-in to record the code and modify it as needed.

 

https://helpx.adobe.com/au/photoshop/kb/downloadable-plugins-and-content.html

 

quote

Is there already a script that does this ?

 

Not that I can think of off-hand, have you searched?