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

Replacing Multiple Smart Objects via Script - Wall Art Mockup

Explorer ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

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.
TOPICS
Actions and scripting , Experiment , Windows

Views

4.6K

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

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? 

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

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

 

 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

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«. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

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

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Did you amend the name? 

Screenshot 2023-05-10 at 10.30.27.png

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Yes I also tried that but again it didn't work

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

What is the line

  subDoc.paste();

supposed to do? 

 

This should replace the Smart Objects’ contents with the jpgs. 

// 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(i+"\nCould not find layer '" + smartObjName + "'.");
    continue;
  }

  // Activate the smart object layer and open the contents
  doc.activeLayer = smartObj;
  replaceContents (inputFolderPath + "/artwork" + i + ".jpg");
  /*// Get a reference to the active document and place the corresponding image
  var subDoc = openSmartObject();
  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);
////// replace contents //////
function replaceContents (theName) {
  var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
  var desc244 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  desc244.putPath( idnull, new File( theName ) );
  executeAction( idplacedLayerReplaceContents, desc244, DialogModes.NO );
};

 Screenshot 2023-05-10 at 10.50.23.png

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Holly! I think We you have accomplished 90% of it. But I dont understand that are those drawings in "first 1"? When i open smart object I see the correct picture seems to be fit to smart object but it diseppears when I close it? 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

And when I try to save and close the smart object, this happens. I uploaded error

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

quote

And when I try to save and close the smart object, this happens. I uploaded error

A jpg cannot have Layers (edit: other than the Background Layer). 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied


@Sam-90 wrote:

Holly! I think We you have accomplished 90% of it. But I dont understand that are those drawings in "first 1"? When i open smart object I see the correct picture seems to be fit to smart object but it diseppears when I close it? 


That’s why I said you should correct the template file so that the Smart Objects and the replacement images have the same dimensions. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Great! Can you guide me on the steps I need to do from now on? I think we can manage and use the script that you shared with just little fix

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Anyway, here is a version that places the jpg as a Smart Object in the Smart Object. 

Depending on your Preferences it should scale the placed jpg to the Canvas when placing. 

// Set the path to the input folder
var inputFolderPath = "C:\\Users\\Sami Ersoy\\Desktop\\Bulk Mockups wall art\\Input\\";
var inputFolderPath = File(app.activeDocument.fullName).parent.fullName;

// 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(i+"\nCould not find layer '" + smartObjName + "'.");
    continue;
  }

  // Activate the smart object layer and open the contents
  doc.activeLayer = smartObj;
//  replaceContents (inputFolderPath + "/artwork" + i + ".jpg");
  // Get a reference to the active document and place the corresponding image
  var subDoc = openSmartObject();
  var inputFilePath = inputFolderPath + "/artwork" + i + ".jpg";
  var inputImgFile = new File(inputFilePath);
  placeScaleRotateFile (inputFilePath, 0, 0, 100, 100, 0)
  // Close and save the smart object contents
  subDoc.close(SaveOptions.SAVECHANGES);
};

// Close the original document without saving
//doc.close(SaveOptions.DONOTSAVECHANGES);
////// replace contents //////
function replaceContents (theName) {
  var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
  var desc244 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  desc244.putPath( idnull, new File( theName ) );
  executeAction( idplacedLayerReplaceContents, desc244, DialogModes.NO );
};
////// open smart object //////
function openSmartObject () {
  var desc2 = new ActionDescriptor();
  executeAction( stringIDToTypeID( "placedLayerEditContents" ), desc2, DialogModes.NO );
  return activeDocument;
  };
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
  var desc5 = new ActionDescriptor();
  desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
  desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
  var idOfst = charIDToTypeID( "Ofst" );
  var desc6 = new ActionDescriptor();
  var idPxl = charIDToTypeID( "#Pxl" );
  desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
  desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
  var idOfst = charIDToTypeID( "Ofst" );
  desc5.putObject( idOfst, idOfst, desc6 );
  var idPrc = charIDToTypeID( "#Prc" );
  desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
  desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
  desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );	
  desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
  executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
  return app.activeDocument.activeLayer;
  };

 Screenshot 2023-05-10 at 11.24.58.png

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

This is last last error

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Sorry, forgot to delete the line 

var inputFolderPath = File(app.activeDocument.fullName).parent.fullName;

that I used to direct to the jpgs; try deleting it. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Thanks Now it worked till it starts "smart object 5" then it gave error. Please check the upload

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

I asked you to provide a set of replacement images – you did not do that but only supplied 2. 

So I have no way to determine what the problem is, if the replacement file »artwork5.jpg« exists for example. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

here you may find replacement images. But name of the images are not important because imagine that you have 1000 images and you will have to group all of these set of 6. So script shouldn't consider names. It should start process from first image in the folder and move on to the other. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

Works here. 

Screenshot 2023-05-10 at 13.25.37.png

 

quote

But name of the images are not important because imagine that you have 1000 images and you will have to group all of these set of 6.

Your Script identifies the replacement images by name specifically so you need to restructure that; get the Files from the Folder and then process those. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

I think I can kiss you know. How do i thank you?! Thank you very much friend for all your help. How do I reach you 🙂

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

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. 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

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

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

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

 

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 ,
May 10, 2023 May 10, 2023

Copy link to clipboard

Copied

LATEST

Thank you very much. Now I will try use your script as template and add auto resize images to fit smart object without spaces. Then I will use this template to use it on my other PSD files. I think best way would be to make all smart object names in each PSD file same to match with current script.

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