Photoshop Scripts for Image Layers (Javascript)
Copy link to clipboard
Copied
Hi,
I have some code which opens a template file, then code below which opens another file. It then duplicates the layer on the second file to original file opened.
var fileRef = new File("/z/FILE.psd");
if(fileRef.exists){
app.open (fileRef);
app.activeDocument.activeLayer.duplicate (app.documents.getByName("psd.psd"));
app.activeDocument = app.documents.getByName("FILE.psd");
app.activeDocument.activeLayer.name = "none.psd";
I have a couple of requirements.
1. Once it has copied, i would like the second file to then close the file down i tried below but didnt work:
this.closeDoc(true)
2. When the image is copied it places it top left hand corner, but i need to adjust the H and V axis to position it within the document.
Can anyone help with this?
Thank you.
Explore related tutorials & articles
Copy link to clipboard
Copied
I managed to use this code to close
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
and found this thread https://forums.adobe.com/thread/2372960
- #target photoshop;
- if(documents.length) main();
- function main(){
- var X = 300; //Top left
- var Y = 300; //Top left
- try{
- var doc = app.activeDocument;
- app.displayDialogs = DialogModes.NO;
- var strtRulerUnits = app.preferences.rulerUnits;
- var strtTypeUnits = app.preferences.typeUnits;
- app.preferences.rulerUnits = Units.PIXELS;
- app.preferences.typeUnits = TypeUnits.PIXELS;
- activeDocument.activeLayer = activeDocument.artLayers.getByName("Layer 1");
- var LB = activeDocument.activeLayer.bounds;
- activeDocument.activeLayer.translate(X-LB[0].value,Y-LB[1].value);
- }catch(e){alert(e + "\nLine number = " + e.line);}
- finally{
- app.preferences.rulerUnits = strtRulerUnits;
- app.preferences.typeUnits = strtTypeUnits;
- }
- };
and the above code moved the canvas
Copy link to clipboard
Copied
That script you posted mover layer 1 not the canvas. The canvas top left pixels is 0X,0Y the bottom right is WidtpxX,HeightpxY The Script moves the active layer, Layer 1 top left to canvas position 300x,300y.
You can get the Document Canvas Bounds and you can get the layer bounds and you can set as selection to a pixel and you can align the layer horizontally and vertically relative to that pixel selected. A Photoshop Script can position a layer many ways using different Photoshop tools.
Here is a quite simple script that will place in a watermark file into a document resize the watermark smart object layer relative to the Document height then position the resized layer to the bottom left or right of the document.
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// ======================================================= */
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
/*
<javascriptresource>
<about>$$$/JavaScripts/PlaceWatermark/About=JJMack's PlaceWatermark ^r^rCopyright 2017 Mouseprints.net^r^rPhotoshop Script^rCustomize using first four var</about>
<category>JJMack's Script</category>
</javascriptresource>
*/
#target photoshop;
app.bringToFront();
var logoFile = "~/Desktop/JJMack.png"; // Watermark file should be large for resize down works better than up
var LogoSize = 10; // percent of document height to resize Watermark to
var LogoMargin = 1; // percent of Document height the Watermark should have as a margin
var BottomLetf = false; // false = Bottom Right true Bottom Left
placeWatermark(logoFile, LogoSize, LogoMargin); // Place Watermark into the bottom of the document
function placeWatermark(Image,Size,Margin){
if(!documents.length) return; // if no document return
try{
var doc = app.activeDocument; // set Doc object to active document
app.displayDialogs = DialogModes.NO; // Dialog off
var strtRulerUnits = app.preferences.rulerUnits; // Save Users ruler units
var strtTypeUnits = app.preferences.typeUnits; // Save Users Type units
app.preferences.rulerUnits = Units.PIXELS; // work with pixels
app.preferences.typeUnits = TypeUnits.PIXELS; // work with pixels
var fileObj = new File(Image); // the passed file
if(!fileObj.exists){ // If file does not exits tell user
alert(fileObj.name + " does not exist!");
return;
}
var layers = app.activeDocument.layers; // get layers
app.activeDocument.activeLayer = layers[0] // Target Top Layer
placeFile(fileObj); // Place in file the Watermark png file
activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer
var SB = activeDocument.activeLayer.bounds; // get layers bounds
var layerHeight = SB[3] - SB[1]; // get layers height
var resizePercent = (100/layerHeight)*(Size/100*doc.height.value); // Percent to resize by
activeDocument.activeLayer.resize(resizePercent ,resizePercent,AnchorPosition.MIDDLECENTER); // Resize width and height by percentage
SB = activeDocument.activeLayer.bounds; // get resized layers bounds
activeDocument.activeLayer.translate(-SB[0].value,-SB[1].value); // Move resized layer to top left canvas corner
var LayerWidth = (SB[2].value - SB[0].value); // get resized layers width
var LayerHeight = (SB[3].value - SB[1].value); // get resized layers height
marginSize = Margin/100*doc.height.value; // Margin size
// move resized watermark into the document lower right corner with some margin or lower left
if ( BottomLetf) {activeDocument.activeLayer.translate(marginSize,( doc.height.value -marginSize - LayerHeight));}
else {activeDocument.activeLayer.translate((doc.width.value -marginSize - LayerWidth),( doc.height.value -marginSize - LayerHeight));}
}
catch(e) { alert(e + ': on line ' + e.line); } // inform user of error
finally{
app.preferences.rulerUnits = strtRulerUnits; // Restore user ruler units
app.preferences.typeUnits = strtTypeUnits; // Restore user type units
};
};
function placeFile(placeFile) {
var desc21 = new ActionDescriptor();
desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
var desc22 = new ActionDescriptor();
desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
};
Copy link to clipboard
Copied
When you open files, assign it a variable:
var firstDoc = app.activeDocument;
var secondDoc = app.open(fileRef)//as you have in your case
//close the second:
secondDoc.close(SaveOptions.DONOTSAVECHANGES);
To move the layer calculate the change in distance:
var x = 400//or wherever you want the canvas moved
var y = 300//same thing
//Your moved layer should be the active layer of the current doc, if the second doc closed so...
var movedLayer = firstDoc.activeLayer;
movedLayer.translate(x-movedLayer.bounds[0],y-movedLayer.bounds[1]);
Copy link to clipboard
Copied
Thanks very much for your help guys. See i have two issues with my code which i found out after testing.
- The images being opened, then duplicated and moved are different sizes. So for example depending upon the size of the layer/file being moved, it will position it in a different place each time. Is there a way to move an item to a position based on its centre point so that even if a file is 50px x 50px or 200px x 200px it will still position them both central to a position?
- My code opens a file then duplicates the layer to my main file, but it duplicates the layer as "Layer 1" which a layer is already called that in my main document and causes issues. I need a way if renaming the layer when its duplicated on to the other file so it doesn’t cause issues with those layers.
Copy link to clipboard
Copied
What you see happing is not what you write is happening. Your images most likely have different Print DPI resolutions. Photoshop want to maintain your Images pixel quality so will not ness with the pixels you have for you images. A document can not have more than one resolution. So when you add a new Images layer the has the pixels from the other resolution image its size may look like it has changed. too big or too small. The image has not been changed your have the exact same pixel in both documents. When you scale the layer to match the scale of the image you pasted the image into the number if pixels will be changed and you will lose some image quality.
Centering the pasted layer is easy. However, it will not resolve any scaling issue. Select all to select the image canvas size then use align layers to selection. To the horizontal and vertical center of the selection.
In a script you can rename a layer name is read write and if you use Action manager scriptlistener code to duplicate the layer into the other document you can name the layer that will be created with in the other document as part of the layer duplicate process like you can using Photoshop Layer>Duplicate UI. And the new layer should be centered. If not You can move the layer and align to the canvas center using a select all and align layers to selection.
Copy link to clipboard
Copied
As I mentioned before, if you use the duplicate line in your code, once you close the file that had the original layer, PS will then select the document that you transferred that file to, and that layer will be the active layer. You can then rename that layer and/or assign it to a variable, without having to use getByName to select the layer.
As far as finding the midpoint, that easy using the layer bounds. For X, just subtract the 0 element in the 2 element; and for Y, the 1 element from 3 element:
var layBounds = app.activeDocument.activeLayer.bounds;
var x= layBounds[2]-layBounds[0];
var y= layBounds[3]-layBounds[1];
Edit: forgot to put "bounds" in line one.
Copy link to clipboard
Copied
Hi, we tried several options but due to the range of sizes of images we couldnt get it right. So in the end we moved all the image on to a new template file with white background all the same size within that file, so that they then always positioned in the same location from image to image.

