Skip to main content
Known Participant
May 9, 2023
Question

Replacing Multiple Smart Objects via Script - Wall Art Mockup

  • May 9, 2023
  • 2 replies
  • 13101 views

Hi, I need a Photoshop script for my Wallaart mockup PSD files. I summarise the necessary steps for you below.

  1. The script will work from my previously opened PSD file
  2. there are 6 smart objects in PSD file
  3. In the input folder, there are 6 jpg image files.
  4. The script will go through this input folder and place these inside of 6 smart objects. 
  5. As images and smart objects are of different sizes, there needs to be resizing of images by Width should be increased by 112,3% and height should be increased by 112,3%..
  6. I've uploaded a screenshot of my PSD file. 

    thank you

    Note: I've read almost all topics regarding Replacing Multiple Smart Objects via Script but I couldn't find any solution for this.
This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
May 10, 2023

You’re welcome. 

 

You could use a function like this 

 

////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
    if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
        return true
        };
	};

 

 to get an Array of the Files in the Folder. 

The you can either use an additional for-clause or a while clause or … to iterate through those Files in »groups« of six. 

Sam-90Author
Known Participant
May 10, 2023

where do i copy and paste it. And can you give me an example to use it? Sorry for my english

c.pfaffenbichler
Community Expert
May 10, 2023

Actually that function isn’t necessary, this should produce an alert that lists the jpg, tif and psd-files in the Folder. 

var inputFolderPath = "C:\\Users\\Sami Ersoy\\Desktop\\Bulk Mockups wall art\\Input\\";
var theFiles = Folder(inputFolderPath).getFiles(/\.(jpg|tif|psd)$/i)
alert ("the folder contains these jpgs, tifs and psds:\n"+theFiles.join("\n"));

 

c.pfaffenbichler
Community Expert
May 9, 2023

From the standpoint of automation that sound like an incorrect way to set up a template. 

The objects and their replacements should be of identical dimensions, otherwise the process gets unnecessarily complicated. 

So I would recommend you correct the template and use an excisting Script (if you could find one that meets your needs aside from the sizing issue, that is). 

 

But I am not completely sure about the specifics so please provide the template and at least one set of replacement images. 

What is the naming-convention for the replacement images and their relation to the Smart Objects in the template? 

Sam-90Author
Known Participant
May 9, 2023

Okay lets say that before start these process first I have adjusted the dimensions of the photos to the same size as the smart objects in which they will be placed.

Now what? I've tried to write some code but as I'm not good at javascript, I can't find the problem in the code.

This is my first code

// Set the path to the input folder
var inputFolderPath = "C:\\Users\\Sami Ersoy\\Desktop\\Bulk Mockups wall art\\Input\\";

// Get a reference to the active document
var doc = app.activeDocument;

// Loop through each smart object layer and place the corresponding image
for (var i = 1; i <= 6; i++) {
  var smartObjName = "ARTWORK " + i + " - 3:4";
  var smartObj = doc.layers.getByName(smartObjName);

  // Check if the smart object layer was found
  if (!smartObj) {
    alert("Could not find layer '" + smartObjName + "'.");
    continue;
  }

  // Activate the smart object layer and open the contents
  doc.activeLayer = smartObj;
  app.executeAction(charIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO);

  // Get a reference to the active document and place the corresponding image
  var subDoc = app.activeDocument;
  var inputFilePath = inputFolderPath + "artwork" + i + ".jpg";
  var inputImgFile = new File(inputFilePath);
  subDoc.activeLayer = subDoc.layers[0];
  subDoc.activeLayer.name = "Smart Object Contents";
  subDoc.activeLayer.bounds = subDoc.bounds;
  subDoc.paste();

  // Close and save the smart object contents
  subDoc.close(SaveOptions.SAVECHANGES);
}

// Close the original document without saving
doc.close(SaveOptions.DONOTSAVECHANGES);


and this is the second

// Set the input and output folders
var inputFolder = new Folder("C:/Users/Sami Ersoy/Desktop/Bulk Mockups wall art/Input");
var outputFolder = new Folder("C:/Users/Sami Ersoy/Desktop/Bulk Mockups wall art/Output");

// Get all the input files in the folder
var inputFiles = inputFolder.getFiles("*.jpg");

// Loop through the input files and process each mockup
for (var i = 0; i < inputFiles.length; i += 6) {
  // Loop through the layers and place the input images in the smart objects
  var layers = app.activeDocument.layers;
  for (var j = 0; j < layers.length; j++) {
    var layer = layers[j];

    // Check if the layer is a smart object
    if (layer.kind == LayerKind.SMARTOBJECT) {
      var inputIndex = i + parseInt(layer.name) - 1;

      // Check if there is an input image for this smart object
      if (inputIndex < inputFiles.length) {
        var inputImg = inputFiles[inputIndex];

        // Place the input image into the smart object
        app.activeDocument.activeLayer = layer;
        app.doAction("Place", "Default Actions");
      }
    }
  }

  // Save the output file with a new name in the output folder
  var outputFile = new File(outputFolder + "/mockup_" + (Math.floor(i / 6) + 1).toFixed(0).padStart(3, '0') + ".jpg");
  app.activeDocument.saveAs(outputFile);
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

 

 

Sam-90Author
Known Participant
May 10, 2023

Please provide the template and at least one set of replacement images. 

 

You seem to have named the Layer »ARTWORK 1 - 3:4« wrong and removed the space before »3«. 


Thank you sir, here you may find the PSD file and 2 images in we transfer link. I look forward to hearing from you :)) To be honest, I got really excited now 🙂


https://we.tl/t-4bNIeMgKVc