Skip to main content
Known Participant
December 2, 2023
Question

Migrated Content

  • December 2, 2023
  • 2 replies
  • 541 views

a

This topic has been closed for replies.

2 replies

Known Participant
December 3, 2023

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();

 

Stephen Marsh
Community Expert
Community Expert
December 3, 2023

Photoshop isn't Photopea, it's is clear that you want different behaviour, however, this isn't a bug.

 

If the replacement image is larger than the PSB placeholder, then it can be placed, resizing to the canvas.

 

Have you considered using the Frame tool instead?

 

https://helpx.adobe.com/au/photoshop/using/tool-techniques/frame-tool.html

 

https://helpx.adobe.com/au/photoshop/using/place-image-frame-tool.html

 

Another option is to use the BatchOneImageCollage.jsx script (which doesn't use Smart Objects) from the late JJMacksPhotoCollageToolkit.

 

https://github.com/MarshySwamp/JJMack-Archive

 

https://web.archive.org/web/20210419210422/http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html

 

Yet another option is Photoshop's Image > Variables (Data Driven Graphics) using pixel replacement and fitting options:

 

https://helpx.adobe.com/photoshop/using/creating-data-driven-graphics.html

 

P.S. If you upload your layered PSD template and 2 or 3 example replacement files it may help those interested in helping.