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

Actions/Scripts: Move layer to absolute position instead of translate

New Here ,
Nov 30, 2017 Nov 30, 2017

Hey all,

Im trying to write a script which takes a layer and moves it to a x/y coordinate on the canvas rather than translates the object a certain amount of pixels. Ive created an action so far and converted it to a script file to try and tweak it but am having trouble understanding the API. I was hoping someone here might be able to help.

Here is the step that I need to adjust from a translate to an absolute position:

  // Transform

  function step9(enabled, withDialog) {

   if (enabled != undefined && !enabled)

   return;

   var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

   var desc1 = new ActionDescriptor();

   var ref1 = new ActionReference();

   ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));

   desc1.putReference(cTID('null'), ref1);

   desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSAverage"));


   var desc2 = new ActionDescriptor();

   desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), 0);

   desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), 1800);

   desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);


   var desc3 = new ActionDescriptor();

   desc3.putEnumerated(sTID("warpStyle"), sTID("warpStyle"), sTID("warpNone"));

   desc3.putDouble(sTID("warpValue"), 0);

   desc3.putDouble(sTID("warpPerspective"), 0);

   desc3.putDouble(sTID("warpPerspectiveOther"), 0);

   desc3.putEnumerated(sTID("warpRotate"), cTID('Ornt'), cTID('Hrzn'));

   desc3.putInteger(sTID("uOrder"), 4);

   desc3.putInteger(sTID("vOrder"), 4);

   desc1.putObject(cTID('warp'), cTID('warp'), desc3);

   desc1.putEnumerated(cTID('Intr'), cTID('Intp'), cTID('Bcbc'));

   executeAction(cTID('Trnf'), desc1, dialogMode);

  };

Thank you in advance for the help!

TOPICS
Actions and scripting
2.9K
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
Adobe
People's Champ ,
Nov 30, 2017 Nov 30, 2017

Moves the top-center point of the layer to the specified position in centimeters or pixels.

///////////////////////////////////////////////////////////////////////////////////////////

function move_cm(x, y)

    {

    var old_units = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.CM;

    try {

        var bounds = app.activeDocument.activeLayer.bounds;

        var off_x = x - (bounds[2].value+bounds[0].value)/2;

        var off_y = y - bounds[1].value;

        var d1 = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

        d1.putReference( charIDToTypeID( "null" ), r );

        var d2 = new ActionDescriptor();

        d2.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Rlt" ), off_x*72/2.54 );

        d2.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Rlt" ), off_y*72/2.54 );

        d1.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Ofst" ), d2);

        executeAction( charIDToTypeID( "move" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    app.preferences.rulerUnits = old_units;

    }

///////////////////////////////////////////////////////////////////////////////////////////

function move_px(x, y)

    {

    var old_units = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.PIXELS;

    try {

        var bounds = app.activeDocument.activeLayer.bounds;

        var off_x = x - (bounds[2].value+bounds[0].value)/2;

        var off_y = y - bounds[1].value;

        var d1 = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

        d1.putReference( charIDToTypeID( "null" ), r );

        var d2 = new ActionDescriptor();

        d2.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Pxl" ), off_x );

        d2.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Pxl" ), off_y );

        d1.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Ofst" ), d2);

        executeAction( charIDToTypeID( "move" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    app.preferences.rulerUnits = old_units;

    }

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
Enthusiast ,
Nov 30, 2017 Nov 30, 2017

This moves left top selected layer(s) to certain pixel position.

Sorry for Humanizer dependency. Maybe somebody could rewrite this into pure AM code.

Anyway this is transform action. But "move" action from r-bin might be 2x faster. But you need to calculate with document DPI if differs from 72DPI.

#include Humanizer.jsx

//https://github.com/jardicc/ActionManagerHumanizer

var desc =

        {

            "null": {

                    "_enum": "ordinal",

                    "_ref": "layer",

                    "_value": "targetEnum"

                },

            "snapToDocBounds": true,

            "relative": false,

            "position": {

                "_obj": "position",

                "_value": {

                    "horizontal": {

                            "_unit": "pixelsUnit",

                            "_value": 100

                        },

                    "vertical": {

                            "_unit": "pixelsUnit",

                            "_value": 100

                        }

                }

            }

        }

   

var descMod = Humanizer.objectToDescriptor(desc)[1];

executeAction( stringIDToTypeID( "transform" ), descMod, 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
People's Champ ,
Nov 30, 2017 Nov 30, 2017

Functions work regardless of the reasolution of the file

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
Enthusiast ,
Nov 30, 2017 Nov 30, 2017
LATEST

I think I understand. You are specifying units and you are using a bit different action descriptor.

I confused this:

var desc = {

            "null": {

                    "_enum": "ordinal",

                    "_ref": "layer",

                    "_value": "targetEnum"

            },

            "to": {

                "_obj": "point", // probably DPI dependency

                "horizontal": 40,

                "vertical": 50

            },

            "suppressPlayLevelIncrease": true

        }

var descMod = Humanizer.objectToDescriptor(desc)[1];

var data = executeAction( stringIDToTypeID( "move" ), descMod, DialogModes.NO );  

With yours:

var desc = {

    "null": {

        "_enum": "ordinal",

        "_ref": "layer",

        "_value": "targetEnum"

    },

    "to": {

        "_obj": "offset",

        "horizontal": {

            "_unit": "distanceUnit",

            "_value": 40

        },

        "vertical": {

            "_unit": "distanceUnit", //might be in pixel units

            "_value": 50

        }

    }

}

var descMod = Humanizer.objectToDescriptor(desc)[1];

var data = executeAction( stringIDToTypeID( "move" ), descMod, 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