joonaspaakko / Batch-Mockup-Smart-Object-Replacement-photoshop-script

Batch Mockup Smart Object Replacement - Photoshop script - A script that can batch process multiple mockup files and is able to replace multiple smart objects per mockup.
95 stars 22 forks source link

Can't get the script to work #34

Open cavalia88 opened 3 months ago

cavalia88 commented 3 months ago

I downloaded your script but couldn't get it to work. I'm using Photoshop 2023. The script runs and attempts to replace the smart object but the output doesn't reflect the change. Script keeps asking whether I want to save the interim psd file.

It's a very simple psd. Two layers, one background and one smart object. Just can't get it to work. Attached the project folder for your reference. Wondering what exactly is wrong.

https://drive.google.com/file/d/14M3RVTP6P5IbRsSkD18B87qztjCcHRV-/view?usp=sharing

joonaspaakko commented 3 months ago

@cavalia88, I took a peek at the settings script and I see no issues, my guess is there's something in the mockup that doesn't match the settings script. Is the smartObjects.target layer in the mockup also called: Replace, with no other words or characters in the layer name? Another thing that comes to mind, having not seen the mockup PSD yet; this target layer has to be a smart object or it won't work.

I don't have PS installed right now, so it may take a while before I can take a proper look at it.

cavalia88 commented 3 months ago

@joonaspaakko Yes, the target layer is a smart object. Please see attached screenshot.

Screenshot Smart Object Layer

joonaspaakko commented 3 months ago

That seems to match with the settings script. I'm going to have to try to run it (hopefully sometime soon).

In the meantime you could try to download the standalone example and first of all run it to see that it works. Then if it does, change things one by one with your of files and settings, all the while testing (running) it so that maybe you can spot where it stops working and why.

cavalia88 commented 3 months ago

Unfortunately, I could not get the standalone example to work on my side. In the end, I asked Chatgpt to help me write a new script and it worked perfectly. Appended the code below for reference (in case anyone encounters the same issue).

#target photoshop

// Function to replace the content of a smart object
function replaceSmartObjectContents(newFile) {
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc.putPath(idnull, new File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc.putInteger(idPgNm, 1);
    executeAction(idplacedLayerReplaceContents, desc, DialogModes.NO);
}

// Function to export the current document as PNG
function exportPNG(outputPath) {
    var opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.PNG;
    opts.PNG8 = false; // set to true for PNG-8
    opts.transparency = true;
    opts.interlaced = false;
    opts.quality = 100;
    var saveFile = new File(outputPath);
    app.activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, opts);
}

// Main function to process files
function processFiles(inputFolderPath, outputFolderPath, smartObjectLayerName) {
    var inputFolder = new Folder(inputFolderPath);
    var outputFolder = new Folder(outputFolderPath);

    if (!outputFolder.exists) {
        outputFolder.create();
    }

    var files = inputFolder.getFiles(/\.(jpg|jpeg|png|tif|tiff)$/i);
    var doc = app.activeDocument;

    for (var i = 0; i < files.length; i++) {
        var file = files[i];

        // Find and select the smart object layer
        var layerFound = false;
        for (var j = 0; j < doc.artLayers.length; j++) {
            if (doc.artLayers[j].name === smartObjectLayerName) {
                doc.activeLayer = doc.artLayers[j];
                layerFound = true;
                break;
            }
        }

        if (!layerFound) {
            alert("Smart Object layer named \"" + smartObjectLayerName + "\" not found.");
            return;
        }

        // Replace smart object content
        replaceSmartObjectContents(file.fsName);

        // Export to PNG
        var outputFilePath = outputFolderPath + "/" + file.name.replace(/\.[^\.]+$/, ".png");
        exportPNG(outputFilePath);
    }
}

// Prompt user for input and output folders
var inputFolder = Folder.selectDialog("Select the folder with the images to replace the smart object content");
var outputFolder = Folder.selectDialog("Select the folder to save the PNG files");

if (inputFolder && outputFolder) {
    // Prompt user for the name of the smart object layer to replace
    var smartObjectLayerName = prompt("Enter the name of the smart object layer to replace:", "Smart Object");

    if (smartObjectLayerName) {
        app.displayDialogs = DialogModes.NO; // Disable dialogs
        app.preferences.rulerUnits = Units.PIXELS; // Set ruler units to pixels

        processFiles(inputFolder.fsName, outputFolder.fsName, smartObjectLayerName);

        app.displayDialogs = DialogModes.ALL; // Enable dialogs
    } else {
        alert("Smart object layer name not provided.");
    }
} else {
    alert("Input or output folder not selected.");
}

Instructions: -Save the Script: Save the script as batch_replace_smart_object.jsx. -Open Photoshop: Open Adobe Photoshop. -Open Your PSD File: Open the PSD file that contains the smart object layer you want to replace. -Run the Script: Go to File > Scripts > Browse and select the batch_replace_smart_object.jsx script. -Select Folders: The script will prompt you to select the input folder containing the images and the output folder where the PNG files will be saved. -Enter Smart Object Layer Name: Enter the name of the smart object layer in your PSD file that you want to replace.

This script will replace the content of the specified smart object layer with each image in the input folder and save the resulting image as a PNG in the output folder. Make sure your smart object layer names match exactly what you input, including any spaces or special characters.