Copy link to clipboard
Copied
Hello,
I'm new to these formus and to extendScript in particular, and I would be verry happy if someone could point out what I'm doing wrong.
I have a script that opens a image (lets call it icon), and for all the images in a folder, resizes icon to the current folder image, pastes the image on top, and saves it.
It works great except for a small problem which I can't seem to fix. When the image from the folder is smaller than the canvas ej:
Then when the image gets copied over the icon image, it's pasted centered, like this:
Which defeats the purpose of what I wanted to do, I needed it pasted in the same position as it was in the original image. As we are talking of well over 1k images, doing this by hand is a bit problematic, so I would be really thankful if someone could help me out.
Here is the script:
#target photoshop
app.bringToFront();
function main()
{
var iconFile = File.openDialog ("Select the base (icon0) file", "*.png", false);
var inputFolder= Folder.selectDialog ("Please select folder to process");
if(inputFolder == null) return;
var fileList = inputFolder.getFiles("*.png");
var outFolder = Folder(inputFolder +"/Processed");
if(!outFolder.exists) outFolder.create();
// Open icon file and create a new layer, making the background one invisible
var icon = open(iconFile);
var artLayerRef = icon.artLayers.add()
icon.layers[1].visible = false;
for(var z in fileList)
{
// Open icon file
var icon = open(iconFile);
var artLayerRef = icon.artLayers.add()
icon.layers[1].visible = false;
// Open 32 bit file
var img32 = open(fileList
var img32Name = decodeURI(fileList
// select all document
img32.selection.selectAll();
img32.selection.copy();
// Copy image to the icon layer
activeDocument = icon;
icon.resizeImage (img32.width.value, img32.height.value);
icon.paste();
// Save icon with name
var saveFile= File(outFolder + "/" + img32Name + ".png");
Save24 (icon, saveFile);
// Clear layer, close open image
icon.selection.selectAll();
icon.selection.clear();
img32.close();
}
icon.close(SaveOptions.DONOTSAVECHANGES);
}
main();
function Save24(doc, saveFile)
{
pngSaveOptions = new PNGSaveOptions();
doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
1 Correct answer
Right, you first get the original position of the activeLayer in img32. After the copy/paste you then translate the pasted layer back to the origianl positon.
Something like this
var img32 = open(fileList
var img32Name = decodeURI(fileList
var x = img32.activeLayer.bounds[0.value];// get the x position of the layer
var y = img32.activeLayer.bounds[1.value];// get the y position of the layer
// selectAll, copy, switch to icon, resize, paste code here
// ...
// after the
...Explore related tutorials & articles
Copy link to clipboard
Copied
You might want to try using layer.duplicate() to copy the layer to the other document. That should maintain the position better.
If that doesn't work you will need to get the position of the layer after it has been copied then use layer.translate() to move it where you want it to appear.
Copy link to clipboard
Copied
Hy Michael, thank you for your reply.
Attempted to layer.duplicate and I got an error saying "general photoshop error, feature not supported in this version" or something along those lines (using Photoshop CS 5).
Adter that, I have been attempting to move the graphics once copied but I don't quite manage to sort out how to do it.
Can I somehow get the original position of the graphics in the first document, to move them to that position on the second one? A script example would really help, as the documentation I'm finding is not too explicit.
Thank you again.
Copy link to clipboard
Copied
Layer.duplicate works in CS5 but we can set that aside for now.
I am not sure what you are trying to position. I skimed your code the frist time and thought that the icon was the image being pasted. img32 will always be centered because you are pasting it into a document that has been sized to fit img32.
So I am guessing that it is the icon you want to position. That is being moved by the resizeImage commad as it is not pasted. If you switch resizeImage for resizeCanvas you can set the anchor positon.
Or you can get the current positon of the icon layer from that layer's bounds. The first two elements of that array are the x,y position of the layer. Later you can move the icon layer using the function below.
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);
}
Copy link to clipboard
Copied
Hy Michael,
first, thank you for taking time to reply. I apologize for how confusing the post is, I realize how hard it is to understand by code, so I'll try it with images to make it clearer.
Here is the original icon file:
// Open icon file
var icon = open(iconFile);
var artLayerRef = icon.artLayers.add()
icon.layers[1].visible = false;
And now we open the first img32 file (sorry for the size, it's a bit hard to see but there is a small green hill on bottom left)
// Open 32 bit file
var img32 = open(fileList
var img32Name = decodeURI(fileList
Then we select the img32 and copy it:
// select all document
img32.selection.selectAll();
img32.selection.copy();
And then we resize the icon document to fit the img32 size:
// Copy image to the icon layer
activeDocument = icon;
icon.resizeImage (img32.width.value, img32.height.value);
Finaly we paste on the icon the img32 content we had copied:
icon.paste();
Notice how the img32 contents get copied smack in the center? I needed to copy it in it's original position, not the center.
You can probably see the problem with working with translate, as I can't figure out the displacement the image needs. All images I need to work are different sizes and positioned in random positions in the canvas, so I can't hack it to be always bottom right or something like that, I need it in the original position.
Any tips? Your help is greatly appreciated, and frankly, the only hope I have about this whole busines right now.
Thanks again.
Copy link to clipboard
Copied
Right, you first get the original position of the activeLayer in img32. After the copy/paste you then translate the pasted layer back to the origianl positon.
Something like this
var img32 = open(fileList
var img32Name = decodeURI(fileList
var x = img32.activeLayer.bounds[0.value];// get the x position of the layer
var y = img32.activeLayer.bounds[1.value];// get the y position of the layer
// selectAll, copy, switch to icon, resize, paste code here
// ...
// after the paste line move the layer back into position.
positionLayer( icon.activeLayer, x, y );// using the function I posted earlier.
Note that x,y come from the opened img32 document so will change to match each file opened in the loop.
Copy link to clipboard
Copied
Haveing a bad night Mike
the lines should be....
var x = img32.activeLayer.bounds[0].value;// get the x position of the layer
var y = img32.activeLayer.bounds[1].value;// get the y position of the layer
Copy link to clipboard
Copied
A bad night, bad eyesight, or typing in a hurry, not sure which.
Looking at the original script I am not sure why the icon file is being opened at the start of the script and then again inside the for loop. I also don't understand why duplicate didn't work as I still think that would solve the position problem and require fewer lines of code.
Copy link to clipboard
Copied
Thank you all for your replies. Managed to get it to work.

