Skip to main content
Participating Frequently
September 14, 2020
Answered

How to import image in special x,y and resize it using jsx script

  • September 14, 2020
  • 5 replies
  • 2801 views

Hello,

 

I want to import one image with special y,x coordination and resize it using javascript script.

If someone have idea, im trying to find the solution but im not good in js.

 

Thanks you.

This topic has been closed for replies.
Correct answer Chuck Uebele

Here's a script that will ask to open two files. then move the top layer to the first doc and close the second file. It will then resize to 50% (you can change that) and finally move it to x=5 and y=5

 

var mask = '*.*'
var file1 = File.openDialog ('Open primary file', mask, false);
var doc1 = open(file1);
var file2 = File.openDialog ('Open primary file', mask, false);
var doc2 = open(file2);

activeDocument = doc2;
var curLayer = doc2.layers[0];
curLayer.duplicate (doc1.layers[0], ElementPlacement.PLACEBEFORE);
doc2.close (SaveOptions.DONOTSAVECHANGES);
activeDocument = doc1
curLayer = doc1.layers[0];


curLayer.resize(50,50);

var xPrime = 5;
var yPrime =5;


curLayer.translate(xPrime - parseInt(curLayer.bounds[0]),yPrime - parseInt(curLayer.bounds[1]));

5 replies

Chuck Uebele
Community Expert
Community Expert
September 15, 2020

Try this:

/OPEN .PSD FILE
var file1 = Folder('C:/Users/Administrator/Desktop/PhotoshopImg/'+ psdFile +'.psd');
var doc1 = open(file1);

//OPEN TEST IMAGE
var file2 = Folder('C:/Users/Administrator/Desktop/PhotoshopImg/img_test.png');
var doc2 = open(file2);

activeDocument = doc2;
var curLayer = doc2.layers[0];
activeLayer = curLayer;
curLayer.duplicate (doc1.layers[0], ElementPlacement.PLACEBEFORE);

activeDocument = doc1
Participating Frequently
September 15, 2020

Same error that say the duplicate command is not available.. 😕😕

Chuck Uebele
Community Expert
Community Expert
September 15, 2020

I'm having no problem running the script you posted. Can you post a screen shot of of PS after the script runs?

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
September 15, 2020

Here's a script that will ask to open two files. then move the top layer to the first doc and close the second file. It will then resize to 50% (you can change that) and finally move it to x=5 and y=5

 

var mask = '*.*'
var file1 = File.openDialog ('Open primary file', mask, false);
var doc1 = open(file1);
var file2 = File.openDialog ('Open primary file', mask, false);
var doc2 = open(file2);

activeDocument = doc2;
var curLayer = doc2.layers[0];
curLayer.duplicate (doc1.layers[0], ElementPlacement.PLACEBEFORE);
doc2.close (SaveOptions.DONOTSAVECHANGES);
activeDocument = doc1
curLayer = doc1.layers[0];


curLayer.resize(50,50);

var xPrime = 5;
var yPrime =5;


curLayer.translate(xPrime - parseInt(curLayer.bounds[0]),yPrime - parseInt(curLayer.bounds[1]));
Participating Frequently
September 15, 2020

Error: The duplicate command is not available.

 

 

 

/OPEN .PSD FILE
var file1 = Folder('C:/Users/Administrator/Desktop/PhotoshopImg/'+ psdFile +'.psd');
var doc1 = open(file1);

//OPEN TEST IMAGE
var file2 = Folder('C:/Users/Administrator/Desktop/PhotoshopImg/img_test.png');
var doc2 = open(file2);

activeDocument = doc2;
var curLayer = doc2.layers[0];
curLayer.duplicate (doc1.layers[0], ElementPlacement.PLACEBEFORE);

activeDocument = doc1

 

 

 

Here is my code 

Chuck Uebele
Community Expert
Community Expert
September 15, 2020

I would copy the image into the new image, then size it, and finally use translate to move it to the xy position.

Is the image you want to import a single layer?

Participating Frequently
September 15, 2020

I have one image and I want to import new one into this first image then size it and then move it with xy position

 

- LayerOne = First Image

- LayerTwo = Second Image (Resize + xy position)

 

Actually my script in jsx open the first image, but i don't know the rest.

 

https://gist.githubusercontent.com/MarshySwamp/1ef7448b6bb22865ed7b7e1b8a4f521f/raw/c24ff74362de991e384b2e8225b9c53b854e308b/Move%2520Layer%2520Content.jsx

This solution is great to move it, but I need first to import the image..

Stephen Marsh
Community Expert
Community Expert
September 15, 2020

This may not be the best way, however, I would align to the canvas (i.e. upper left), then translate using the required pixel position.

 

Participating Frequently
September 15, 2020

The second is cool, but how I import my image to my PSD?

JJMack
Community Expert
Community Expert
September 14, 2020

import image in special x,y   import one image with special y,x 

 

Before we can address  resize can you explain  special x,y   and  special y,x  ? And Import how?

 

JJMack
Participating Frequently
September 14, 2020

x,y is the coordinates.

Import the image with JSX Script (Javascript Code Script)

 

JJMack
Community Expert
Community Expert
September 15, 2020

Yes what is special about your x,y points. You can align an image layer to any pixel. Select the pixel and align the layer(s) to the selection. Resize the layer and align the image layer to the x,y point.

JJMack