• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script never stopping

Explorer ,
Nov 10, 2024 Nov 10, 2024

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;
}



TOPICS
Actions and scripting , Windows

Views

160

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Nov 10, 2024 Nov 10, 2024

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 11, 2024 Nov 11, 2024

Copy link to clipboard

Copied

The idea is that you run the script, first select the "Scanned Raw" and then the "Cleaned" folder. It should paste the images accordingly.
I tried placing the image directly, but for some reason it will paste them at 50% their original size. So this way pastes the image at their original resolution

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 11, 2024 Nov 11, 2024

Copy link to clipboard

Copied

Do the images have the exact same resolution? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 11, 2024 Nov 11, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines