Photoshop Script batch Smart Object insert/replace ISSUES

191 views Asked by At

I need a Photoshop script to automate the process of inserting PNG files from fold_A = "C:\Users\Utente\Desktop\design" into the active smart object layer. The PNGs should fit proportionally, similar to Photopea's "Generate Mockups" feature (File --> Automate --> "Generate Mockups" with stretch option deselected).

The script should: Open the smart object layer. Insert the PNG, ensuring it fits proportionally (like dragging and dropping into a smart object in Photopea). Save the PSB file. Return to the main PSD file. Export the PSD as a JPG with quality 12 into fold_B = "C:\Users\Utente\Desktop\mockup" using the same name as the inserted PNG. The script should replace the previous PNG in the smart object with the new one, deleting the old PNG from the PSB. alert "FINITO" when finished.

I've tried various codes however the common problem I encounter with every code is that the png won't behave as when I open the smart object clicking on the thumbnail and manually drag and drop the png, which will automatically be fitted inside the canvas of the .psb file. All the codes I've tried will just overlay the png without taking in consideration the smart object dimensions (as if it just drag and drops inside the smart object layer without opening it and saving it)

Here's a video of the wanted outcome: https://streamable.com/5bveni

Images descriptions: 1)bbox of the smart object 2)how it should be inside of smart object (which is what happens when I open SO and drop the png manually) 3)how the mockup should be replacing each png inside of the smart object and saving 4)how script will open make the smart object (wrong) 5)how script creates mockup (wrong)

bbox of the smart object

how it should be inside of smart object (which is what happens when I open SO and drop the png manually)

how the mockup should be replacing each png inside of the smart object and saving

how script will open make the smart object (wrong)

how script creates mockup (wrong)

The issue is that with this code is that the pngs will replace the smart object while they should replace the layer inside of the smart object:

// Create a new window
var win = new Window("dialog", "Process Images");

// Add input folder panel
var inputPanel = win.add("panel", undefined, "Input Folder");
inputPanel.alignment = "fill";
var inputFolderBtn = inputPanel.add("button", undefined, "Select Input Folder");
var inputFolderText = inputPanel.add("edittext", undefined, "");
inputFolderText.alignment = "fill";
inputFolderBtn.onClick = function() {
  inputFolder = Folder.selectDialog("Select a folder with images to process:");
  if (inputFolder) {
    inputFolderText.text = inputFolder.fsName;
  }
};

// Add output folder panel
var outputPanel = win.add("panel", undefined, "Output Folder");
outputPanel.alignment = "fill";
var outputFolderBtn = outputPanel.add("button", undefined, "Select Output Folder");
var outputFolderText = outputPanel.add("edittext", undefined, "");
outputFolderText.alignment = "fill";
outputFolderBtn.onClick = function() {
  outputFolder = Folder.selectDialog("Select a folder to save the processed images:");
  if (outputFolder) {
    outputFolderText.text = outputFolder.fsName;
  }
};

// Add mockup folder panel
var mockupPanel = win.add("panel", undefined, "Mockup Folder");
mockupPanel.alignment = "fill";
var mockupFolderBtn = mockupPanel.add("button", undefined, "Select Mockup Folder");
var mockupFolderText = mockupPanel.add("edittext", undefined, "");
mockupFolderText.alignment = "fill";
mockupFolderBtn.onClick = function() {
  mockupFiles = File.openDialog("Select the PSD mockup files:", "PSD files:*.psd;*.psdt", true);
};

// Add process button
var processBtn = win.add("button", undefined, "Process Images");
processBtn.alignment = "center";
processBtn.onClick = function() {
  if (inputFolder && outputFolder) {
    // Process images
    var imageFiles = inputFolder.getFiles(/\.(jpg|jpeg|png|gif|tiff|webp)$/i);

    for (var m = 0; m < mockupFiles.length; m++) {
        var mockup = app.open(mockupFiles[m]);
        for (var i = 0; i < imageFiles.length; i++) {
            processImage(imageFiles[i], mockupFiles[m]);
        }
        // Close the PSD mockup
        mockup.close(SaveOptions.DONOTSAVECHANGES);
    }

    // Function to process each image
    function processImage(imageFile, mockupFile) {
        // Replace the Smart Object's content with the new image
        var smartObjectLayer = mockup.layers[0]; // Adjust this if the Smart Object is not the first layer
        var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
        var desc = new ActionDescriptor();
        desc.putPath(charIDToTypeID("null"), new File(imageFile));
        executeAction(idplacedLayerReplaceContents, desc, DialogModes.NO);

        // Save the processed image
        var outputFile = new File(outputFolder + "/" + mockupFile.displayName.replace(".psdt", "") + "_" + imageFile.name);
        var saveOptions = new JPEGSaveOptions();
        saveOptions.quality = 12;
        mockup.saveAs(outputFile, saveOptions, true, Extension.LOWERCASE);
    }
    alert("Processing complete.");
  } else {
    alert("Please select input and output folders.");
  }
};

// Function to process each image
function processImage(imageFile, mockup) {
  // Replace the Smart Object's content with the new image
  var smartObjectLayer = mockup.layers[0]; // Adjust this if the Smart Object is not the first layer
  var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
  var desc = new ActionDescriptor();
  desc.putPath(charIDToTypeID("null"), new File(imageFile));
  executeAction(idplacedLayerReplaceContents, desc, DialogModes.NO);

  // Save the processed image
  var outputFile = new File(outputFolder + "/" + mockup.name + "_" + imageFile.name);
  var saveOptions = new JPEGSaveOptions();
  saveOptions.quality = 12;
  mockup.saveAs(outputFile, saveOptions, true, Extension.LOWERCASE);
}

// Add exit button
var exitBtn = win.add("button", undefined, "Exit");
exitBtn.alignment = "center";
exitBtn.onClick = function() {
win.close();
};

// Show the window
win.show();

0

There are 0 answers