Skip to main content
December 9, 2010
Answered

How can get and set x , y position of opend image ?

  • December 9, 2010
  • 3 replies
  • 11343 views

Hi,

I used photoshop scripting in back end.

I  am using javascript.

Here i do following step for image adding in layer.

1. Create main window for save psd file.

2. Create new layer in this window i.e. layer1.
3. Open image in separate new window.
4. Copy image from this window.
5. Set focus (back) in main window and i layer1.
6. Past image here.

Here my image is always set on center of layer1.
And i want to set image in given x,y position in layer1

Is any one help me so i can set this pasted image in given x,y position in layer1 ?

Thanks,
Mohit.

This topic has been closed for replies.
Correct answer Michael_L_Hale

Here is how I would do this( using your cleaned up code )

FinishLayoutToPhotoshop();
function FinishLayoutToPhotoshop()
{
    preferences.rulerUnits = Units.PIXELS;
    var mainDoc = app.documents.add (1024, 1024, 72, 'newdoc', NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1" );
        
    var fileRef = new File("/c/1.jpeg"); // Here 1.jpeg size is 200x200  and 72 dpi
    var sourceDoc = app.open(fileRef);
    sourceDoc.activeLayer.duplicate (mainDoc);
    app.activeDocument = mainDoc;
    positionLayer( mainDoc.activeLayer, 50,50 );
}
function positionLayer( lyr, x, y ){// layerObject, Number, Number
     // if can not move layer return
     if(lyr.iisBackgroundLayer||lyr.positionLocked) return
     // get the layer bounds
     var layerBounds = lyr.bounds;
     // get top left position
     var layerX = layerBounds[0].value;
     var layerY = layerBounds[1].value;
     // the difference between where layer needs to be and is now
     var deltaX = x-layerX;
     var deltaY = y-layerY;
     // move the layer into position
     lyr.translate (deltaX, deltaY);
}

I wouldn't copy and paste. Just dupe the layer then move where needed.

3 replies

Participant
March 27, 2016

A strange problem.

If I have a document A, with a width WIDTH_A into which I want to insert a document B with WIDTH_B, and if WIDTH_B > WIDTH_A then when I pasted B into A and started to resize and  translate, the initial position of B seemed to wander. I fixed the problem by resizing B such that WIDTH_B <= WIDTH_A, then the translations stayed true.

This behaviour also happen when I translated B "off screen" to A. The absolute position of B would wander.

JJMack
Community Expert
Community Expert
December 9, 2010

Mohit M Chavda wrote:

Here i do following step for image adding in layer.

1. Create main window for save psd file.

2. Create new layer in this window i.e. layer1.
3. Open image in separate new window.
4. Copy image from this window.
5. Set focus (back) in main window and i layer1.
6. Past image here.

A couple of things to look at.   The Open PSD documents resolution and pixel size compared to the Image you open resolution and pixel size.  If you paste what you copy to into the clipboard into your PSD document it will be on its own layer.  Remember Photoshop layers can be any size they can be larger/Smaller/Same as the canvas size and can have the same or different aspect ratio as the canvas.  You can retrieve a layers boundaries with scripting as well as resize it and position it.  You may want to look at the scripts in my Photo Collage Toolkit.    These scripts stay within the PSD document and place images in as smart object layers instead of pasting images as normal layers.  It takes a while to understand how Photoshop implemented place. However when pasting in images you will need to address image and documents pixel size. http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html

JJMack
December 9, 2010

Hi ,

First thanks you all for response but i still not find answer of this can you please help me ? , Here i posted sample code of script
--------------------------------------------------------------------------------------------------------------------------------------------
FinishLayoutToPhotoshop();
function FinishLayoutToPhotoshop()
{
    preferences.rulerUnits = Units.PIXELS;
    createApplication(1024,1024,72,"newdoc"); // Current size is 1024 X 1024  and 72 dpi
    addNewLayer();
       
    var fileRef = new File("/c/1.jpeg"); // Here 1.jpeg size is 200x200  and 72 dpi
    app.open(fileRef);
    // Select All
    var idsetd = charIDToTypeID( "setd" );
        var desc263 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref138 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idfsel = charIDToTypeID( "fsel" );
            ref138.putProperty( idChnl, idfsel );
        desc263.putReference( idnull, ref138 );
        var idT = charIDToTypeID( "T   " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idAl = charIDToTypeID( "Al  " );
        desc263.putEnumerated( idT, idOrdn, idAl );
    executeAction( idsetd, desc263, DialogModes.NO );

    app.activeDocument.selection.copy();                // Copy   
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES) // Close
    var curDoc = getCurDoc("newdoc");
    app.activeDocument = curDoc;
    app.activeDocument.paste();   
}
function getCurDoc(namedoc)
{
    var retObj;
     for(var i = 0 ; i < app.documents.length; i++)
     {
            var curDoc = app.documents;
            if(curDoc != null && curDoc.name ==  namedoc)
            {
                    retObj = curDoc;
                    break;
            }           
     }
    return retObj;
}
function createApplication(wwi,whi,wdpi,docname)
{
    var idMk = charIDToTypeID( "Mk  " );
        var desc193 = new ActionDescriptor();
        var idNw = charIDToTypeID( "Nw  " );
            var desc194 = new ActionDescriptor();
            var idNm = charIDToTypeID( "Nm  " );
            desc194.putString( idNm, docname );
            var idMd = charIDToTypeID( "Md  " );
            var idRGBM = charIDToTypeID( "RGBM" );
            desc194.putClass( idMd, idRGBM );
            var idWdth = charIDToTypeID( "Wdth" );
            var idRlt = charIDToTypeID( "#Rlt" );
            desc194.putUnitDouble( idWdth, idRlt, wwi );
            var idHght = charIDToTypeID( "Hght" );
            var idRlt = charIDToTypeID( "#Rlt" );
            desc194.putUnitDouble( idHght, idRlt, whi );
            var idRslt = charIDToTypeID( "Rslt" );
            var idRsl = charIDToTypeID( "#Rsl" );
            desc194.putUnitDouble( idRslt, idRsl, wdpi );
            var idpixelScaleFactor = stringIDToTypeID( "pixelScaleFactor" );
            desc194.putDouble( idpixelScaleFactor, 1.000000 );
            var idFl = charIDToTypeID( "Fl  " );
            var idFl = charIDToTypeID( "Fl  " );
            var idWht = charIDToTypeID( "Wht " );
            desc194.putEnumerated( idFl, idFl, idWht );
            var idDpth = charIDToTypeID( "Dpth" );
            desc194.putInteger( idDpth, 8 );
            var idprofile = stringIDToTypeID( "profile" );
            desc194.putString( idprofile, "sRGB IEC61966-2.1" );
        var idDcmn = charIDToTypeID( "Dcmn" );
        desc193.putObject( idNw, idDcmn, desc194 );
    executeAction( idMk, desc193, DialogModes.NO );

function addNewLayer()
{
    var idMk = charIDToTypeID( "Mk  " );
            var desc191 = new ActionDescriptor();
            var idnull = charIDToTypeID( "null" );
                var ref76 = new ActionReference();
                var idLyr = charIDToTypeID( "Lyr " );
                ref76.putClass( idLyr );
            desc191.putReference( idnull, ref76 );
        executeAction( idMk, desc191, DialogModes.NO );
}

--------------------------------------------------------------------------------------------------------------------------------------------

After executing script here in newDoc's Layer 1 have 1.jpeg image and it's position is arround x = 413  , y =  410   but my requirement is to set this image's x,y position is 50x50 , is there any way using script so we direct give past image and it's position ?


Thanks,

Mohit.

Inspiring
December 9, 2010

In xtools/xlib/stdlib.js there is are functions for dealing with all of this.

Stdlib.insertImageIntoMask(doc, layer, im, fit) ;

Stdlib.insertImageIntoSelection(doc, layer, im, fit);

Stdlib.insertImageIntoSelectionAsSmartObject(doc, layer, im, fit);

where 'im' can be either an open document or a file and where fit indicates whether to fit(true) the image into the area or fill(fit == false) the area with the image.

There may be simpler implementations of this, but these function are fairly robust: I use variants of them in CSX.

All you have to do is define the selection on the target document.

Inspiring
December 9, 2010

Off the top of my head, after you open image 2, get it's size then copy the contents.

Create a selection at X, Y in image 1 with the size info from image 2.

doc.paste(true); should do the trick.

Make sure that you are working in pixels. It makes life much easier.

There are more complicated solutions, if this doesn't work.