Copy link to clipboard
Copied
Hi, I "made" this script that pastes images from a folder to another folders psd's, it works just as I want it to, however it doesn't seem to stop, once it's finished pasting the images is still loading... something(?). I can open documents and work as usual but the script is still running in the background doing god knows what.
So I was wondering if anyone could help me out figure the issue.
Please have in mind I am not a programmer, I don't even know how i got the script to work in the first place... So any additional feedback would be really useful.
Thanks beforehand!!!!
Here's the code, I can't upload the actual jsx file... 😕
var inputFolder = Folder.selectDialog("Select the folder with your scans");
if (!inputFolder) {
alert("Select the folder, dummy.");
throw new Error("No input folder selected.");
}
var outputFolder = Folder.selectDialog("Select the folder with your PSD files");
if (!outputFolder) {
alert("Select a folder, come on, I won't bite ya.");
throw new Error("No output folder selected.");
}
var inputFiles = inputFolder.getFiles(/\.(jpg|png|psd|raw|gif)$/i);
var outputFiles = outputFolder.getFiles(/\.(jpg|png|psd|raw|gif)$/i);
if (inputFiles.length === 0 || outputFiles.length === 0) {
alert("Make sure there are files in both folders.");
throw new Error("Empty folder.");
}
if (inputFiles.length < outputFiles.length) {
alert("Not enough images to cover each PSD. Check your input folder.");
throw new Error("Insufficient images.");
}
OpenFolder();
function OpenFolder() {
var filesOpened = 0;
var imageIndex = 0; // Starting index for images
for (var i = 0; i < outputFiles.length; i++) {
open(outputFiles[i]);
var outputFile = app.activeDocument;
// Determine if the output file is a Double page spread
var isDoubleSpread = outputFile.width > outputFile.height;
if (isDoubleSpread) {
// If DoubleSpread, paste two images
if (imageIndex < inputFiles.length - 1) { // Ensure there's at least two images left
pasteImage(inputFiles[imageIndex]);
pasteImage(inputFiles[imageIndex + 1]);
imageIndex += 2; // Move to the next pair
} else {
alert("Not enough images for a Double page spread PSD. Stopping process.");
break;
}
} else {
// If portrait, paste one image
if (imageIndex < inputFiles.length) {
pasteImage(inputFiles[imageIndex]);
imageIndex++; // Move to the next image
} else {
alert("No more images to paste. Stopping process.");
break;
}
}
app.activeDocument = outputFile;
outputFile.save();
outputFile.close(SaveOptions.DONOTSAVECHANGES);
filesOpened++;
}
return filesOpened;
}
function pasteImage(imageFile) {
var doc = app.activeDocument;
// Open the image file
var image = open(imageFile);
// Duplicate the image to the target document
image.selection.selectAll();
image.selection.copy();
image.close(SaveOptions.DONOTSAVECHANGES);
// Paste the image into the active document
doc.paste();
var pastedLayer = doc.activeLayer;
// Ensure the pasted layer is placed at its original size (no scaling)
pastedLayer.resize(100, 100); // Resize back to 100%, in case Photoshop scaled it
// Optionally, rasterize the layer if you don't want it as a Smart Object
pastedLayer.rasterize(RasterizeType.ENTIRELAYER);
// Rename the layer to ensure uniqueness
pastedLayer.name = getUniqueLayerName("Scanned Version");
}
function getUniqueLayerName(baseName) {
var layerCount = 1;
var uniqueName = baseName;
var layerExists = true;
while (layerExists) {
layerExists = false;
for (var i = 0; i < app.activeDocument.layers.length; i++) {
if (app.activeDocument.layers[i].name === uniqueName) {
layerExists = true;
layerCount++;
uniqueName = baseName + " " + layerCount;
break;
}
}
}
return uniqueName;
}
Copy link to clipboard
Copied
Please provide a set of folders/images for testing. (Feel free to flatten and blacken the images if the content is sensitive.)
Why are you copy/pasting instead of placing?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Do the images have the exact same resolution?
Copy link to clipboard
Copied
Yup, I tested it in 1200px and 2048px tall images and the thing worked fine, but when I tried with 4800px images, it would paste them in half the size, so I wanted a solution that worked in every scenario, and I think this does.