Photoshop script to paste an image in specific x,y loation

New Here ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

Hi All,

 

I have very little knowlidge on javascript. I want a script to be use for mutiple images of difference sizes which needs to be pastes to a new layer in specific x, y location.

 

Thanks

Ed

TOPICS
Actions and scripting, Feature request, How to, Mac, Windows

Views

452

Likes

Translate

Translate

Report

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
New Here ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

 

Likes

Translate

Translate

Report

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
New Here ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

Scale_.jpg

Likes

Translate

Translate

Report

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 Community Professional ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

As I'm only a casual/hobby scripter, I would "cheat" (as I don't know an elegant way).

 

So, first I would select all, then align the active layer to the selection, then deselct. Perhaps you can create the selection in a targeted position so that the object is aligned exactly where you need it from the start:

 

// Align Active Layer to Select All.jsx

/* 
//macscripter.net/viewtopic.php?id=38890

AdLf = Align Left
AdRg = Align Right
AdCH = Align Centre Horizontal
AdTp = Align Top
AdBt = Align Bottom
AdCV = Align Centre Vertical
*/

//www.ps-scripts.com/viewtopic.php?f=66&t=7036&p=35273&hilit=align+layer#p35273

align2SelectAll('AdCH'); align2SelectAll('AdCV'); //Change as required

function align2SelectAll(method) {

   app.activeDocument.selection.selectAll();

   var desc = new ActionDescriptor();
   var ref = new ActionReference();
   ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
   desc.putReference(charIDToTypeID("null"), ref);
   desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
   try {
      executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
   } catch (e) { }

   app.activeDocument.selection.deselect();

}

 

Next, I would move/offset/translate the layer from the previously aligned position:

 

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;        
app.preferences.rulerUnits = Units.PIXELS;
// Move the current layer in 5px from the right and down 5px from the top
app.activeDocument.activeLayer.translate( -5, 5 );
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler; 

 

It may not be elegant, but it should get the job done!

 

An alternative (untested) may be to set the ruler origin to the X/Y position required, then paste or move the content as required in relation to the zero point.

 

/* 
https://community.adobe.com/t5/photoshop/feature-request-set-origin-point/td-p/12037399
Feature Request: Set Origin Point
*/

#target photoshop

if (app.documents.length > 0) {

    (function () {

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

        ////////////////////////////// tom_ruark @ adobe //////////////////////////////

        /* https://feedback.photoshop.com/conversations/photoshop/photoshop-ability-to-ruler-origin-by-script/5f5f45bb4b561a3d425c7b32 */

        // Version 2016.11.18
        // Show how to get and set the ruler origin point for the current document
        // Values are in pixels shifted 16 bits
        // some constants to make it more readable

        const classProperty = app.stringIDToTypeID("property");
        const krulerOriginHStr = app.stringIDToTypeID("rulerOriginH");
        const krulerOriginVStr = app.stringIDToTypeID("rulerOriginV");
        const classDocument = app.stringIDToTypeID("document");
        const typeOrdinal = app.stringIDToTypeID("ordinal");
        const enumTarget = app.stringIDToTypeID("targetEnum");
        const typeNULL = app.stringIDToTypeID("null");
        const keyTo = app.stringIDToTypeID("to");
        const eventSet = app.stringIDToTypeID("set");

        // get the current values
        GetRulerOrigin().toSource();

        function GetRulerOrigin() {
            var ro = {};

            ro.horizontal = GetInfo(classDocument, krulerOriginHStr) >> 16;
            ro.vertical = GetInfo(classDocument, krulerOriginVStr) >> 16;

            return ro;
        }

        function SetRulerOrigin_Horiz(horiz) {
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putProperty(classProperty, krulerOriginHStr);
            ref.putEnumerated(classDocument, typeOrdinal, enumTarget);
            desc.putReference(typeNULL, ref);
            desc.putInteger(keyTo, horiz << 16);
            executeAction(eventSet, desc, DialogModes.NO);
        }

        function SetRulerOrigin_Vert(vert) {
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putProperty(classProperty, krulerOriginVStr);
            ref.putEnumerated(classDocument, typeOrdinal, enumTarget);
            desc.putReference(typeNULL, ref);
            desc.putInteger(keyTo, vert << 16);
            executeAction(eventSet, desc, DialogModes.NO);
        }

        ///////////////////////////////////////////////////////////////////////////////
        // Function: GetInfo
        // Usage:    Get information from Photoshop
        // Input:    desiredClass, classApplication, classLayer, etc.
        //           desiredKey, optional specific key to get instead of everything
        //           this is recommended as all keys is an expensive call
        // Return:   ActionDescriptor or single value depending on what is asked for
        ///////////////////////////////////////////////////////////////////////////////
        function GetInfo(desiredClass, desiredKey) {
            var reference = new ActionReference();
            if (typeof desiredKey != "undefined") {
                reference.putProperty(stringIDToTypeID("property"), desiredKey);
            }
            reference.putEnumerated(desiredClass, stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
            var desc = executeActionGet(reference);
            if (typeof desiredKey != "undefined") {
                return GetItemFromDescriptor(desc, desiredKey);
            }
            return desc;
        }

        ///////////////////////////////////////////////////////////////////////////////
        // Function: GetItemFromDescriptor
        // Usage:    Get a specific key from an ActionDescriptor
        // Input:    desc (ActionDescriptor), valid ActionDescriptor to pull info from
        //           desiredKey (Number), key in question, use charIDToTypeID() or
        //           stringIDToTypeID()
        // Return:   ActionDescriptor or single value depending on what is asked for
        ///////////////////////////////////////////////////////////////////////////////
        function GetItemFromDescriptor(desc, desiredKey) {
            if (desc.hasKey(desiredKey)) {
                var typeID = desc.getType(desiredKey);
                switch (typeID) {
                    case DescValueType.BOOLEANTYPE:
                        return desc.getBoolean(desiredKey);
                        break;
                    case DescValueType.STRINGTYPE:
                        return desc.getString(desiredKey);
                        break;
                    case DescValueType.DOUBLETYPE:
                        return desc.getDouble(desiredKey);
                        break;
                    case DescValueType.INTEGERTYPE:
                        return desc.getInteger(desiredKey);
                        break;
                    case DescValueType.LARGEINTEGERTYPE:
                        return desc.getLargeInteger(desiredKey);
                        break;
                    case DescValueType.OBJECTTYPE:
                        return desc.getObjectValue(desiredKey);
                        break;
                    case DescValueType.UNITDOUBLE:
                        var newT = desc.getUnitDoubleType(desiredKey);
                        var newV = desc.getUnitDoubleValue(desiredKey);
                        return new UnitValue(newV, newT);
                        break;
                    case DescValueType.ENUMERATEDTYPE:
                        return desc.getEnumerationValue(desiredKey);
                        break;
                    case DescValueType.CLASSTYPE:
                        return desc.getClass(desiredKey);
                        break;
                    case DescValueType.ALIASTYPE:
                        return desc.getPath(desiredKey);
                        break;
                    case DescValueType.RAWTYPE:
                        var tempStr = desc.getData(desiredKey);
                        var rawData = new Array();
                        for (var tempi = 0; tempi < tempStr.length; tempi++) {
                            rawData[tempi] = tempStr.charCodeAt(tempi);
                        }
                        return rawData;
                        break;
                    case DescValueType.REFERENCETYPE:
                        return desc.getReference(desiredKey);
                        break;
                    case DescValueType.LISTTYPE:
                        return desc.getList(desiredKey);
                        break;
                    default:
                        return;
                }
            }
            return;
        }

        // Reset ruler origin to zero for a known start point
        SetRulerOrigin_Horiz(0);
        SetRulerOrigin_Vert(0);

        ////////////////////////////// tom_ruark @ adobe //////////////////////////////

        // Set the origin in px
        SetRulerOrigin_Horiz(300); // X value
        SetRulerOrigin_Vert(150); // Y value

        // Restore the ruler units
        app.preferences.rulerUnits = savedRuler;
    })
        ();
}

else {
    alert('A document must be open to use this script!');
}

 

Likes

Translate

Translate

Report

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 ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

How would your script know the x,y position. If the image is too large for the canvas what is your script going to do.  You need to design some process  your script can use for the different conditions it may encounter.  Or you may just need to create a Photo Collage template and have a script populate your various size images into a template.  Free Photoshop Photo Collage and Mockup Toolkit 

 

JJMack

Likes

Translate

Translate

Report

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
New Here ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

Hi JJMack,

When I referring multiple image, I meant batch process where I need to create scale value for multiple image. I am pretty sure that the image would not be bigger than the canvas as the source image would be of lesser resolution. I want to give a value X and Y location and have the image get pasted to that specific location.

 

Something like this

X  = 850

Y = 1600

 

Scale_.jpg

Likes

Translate

Translate

Report

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 ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

LATEST

It sounds like you do not have any programming experience.  Photoshop scripting is programming photoshop.  You would not be pretty sure that the image would not be bigger than the canvas.  Your Program will test and be positive about sizes and process accordingly.  If you have no programming skills Scripting photoshop is not something you can lean quickly.

 

If all you want to do is save your images for various size for print on various paper sizes.  You could simply download the Image Processor Pro Plug-in script and batch process your image files with that script. In a single run for example  it can process all your image files and save up to 10 different size jpeg files of each file processed.  The Image would be canvas size and have the original  image's Aspect Ratios.   These would print centered on your various size papers. 

 

Image Processor Pro is a Photoshop script that X programmed and he made it available on the web for free. It can also include an actions you record for each of your output image sizes. So after the script sized the document for the output image size you you set you can add Canvas  so the image will be on a canvas your paper size the image will be centered if you leave the anchor point centered in the canvas size step.

JJMack

Likes

Translate

Translate

Report

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