Skip to main content
bufumofo
Participating Frequently
April 9, 2019
Answered

Photoshop: Batch Replace Smart Objects - Script Error. Please Help

  • April 9, 2019
  • 4 replies
  • 7902 views

Dear All,

I've been looking for a Photoshop script for a long time, that could help me batch replace images in a smart object (essentially just trying to batch convert flat 1125x750px images to 3D images).

As I'm quite the Photoshop beginner, I had someone create a mockup file and he found an existing script to work with on these forums.

So here's what I have:

  • a mockup file of an image set as a transformed smart object
  • a folder of images resized to fit said smart object

This is what I would like the script to do:

  • for every image in said folder
  • replace smart object with images

Your man sent me a video how to execute the script and it worked just fine on his computer (Windows 7). Problem is that it doesn't work on mine (Windows 7 and Windows 10). I've tried on several computers (some had better specs than his), but unfortunately to no avail. He used a legal version of Photoshop CC 2018 and so did I, so it wouldn't have to do with that. We both can't find the issue.

The error message that I get: ''executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO''. I use the following script:

// Replace SmartObject’s Content and Save as JPG

// 2017, use it at your own risk

// Via @10354895 B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photos...

// JPG code from here: https://forums.adobe.com/thread/737789

#target photoshop

if (app.documents.length > 0) {

    var myDocument = app.activeDocument;

    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

    var thePath = myDocument.path;

    var theLayer = myDocument.activeLayer;

    // JPG Options;

    jpgSaveOptions = new JPEGSaveOptions(); 

    jpgSaveOptions.embedColorProfile = true; 

    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

    jpgSaveOptions.matte = MatteType.NONE; 

    jpgSaveOptions.quality = 8;  

    // Check if layer is SmartObject;

    if (theLayer.kind != "LayerKind.SMARTOBJECT") {

        alert("selected layer is not a smart object")

    } else {

        // Select Files;

        if ($.os.search(/windows/i) != -1) {

            var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)

        } else {

            var theFiles = File.openDialog("please select files", getFiles, true)

        };

        if (theFiles) {

            for (var m = 0; m < theFiles.length; m++) {

                // Replace SmartObject

                theLayer = replaceContents(theFiles, theLayer);

                var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

                // Save JPG

                myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);

            }

        }

    }

};

// Get PSDs, TIFs and JPGs from files

function getFiles(theFile) {

    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {

        return true

    };

};

// Replace SmartObject Contents

function replaceContents(newFile, theSO) {

    app.activeDocument.activeLayer = theSO;

    // =======================================================

    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID("null");

    desc3.putPath(idnull, new File(newFile));

    var idPgNm = charIDToTypeID("PgNm");

    desc3.putInteger(idPgNm, 1);

    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

    return app.activeDocument.activeLayer

};

Please find screenshots attached of the error message that I get + layers and channels at the moment the script fails. Please note that I've blurred out the smart object design as I paid for it - would like to keep it private and not for all to view.

If someone could help me out I'd really REALLY appreciate it. I've literally been caving myself into my apartment for the past 4 days trying to resolve this.

This topic has been closed for replies.
Correct answer JJMack

JJMack - again thank you for your response. As I previously stated, unfortunately I cannot upload the mockup for all to view. For me that would feel like giving away the secret recipe for my entire business. I've been working on this for a very long time. I therefore uploaded it as a password protected file at the link below and PM'ed you the password.

https://gofile.io/?c=L5Cc0j

Sorry if I'm being a hardass - I just hope that you can understand from my point of view and that you would consider looking at it.


Your problem is replacement object files need to be exactly the same size as the object in the mockup template. Same Aspect Ratio, Same number Of pixels  and the same Print Resolution like I wrote.   All I needed to do was to change your two images that had a print resolution of 72DPI to have a 300DPI pint resolution setting then your script worked fine.   What was happening was Photoshop Place was resizing one of your images and in the process used all you machine resources.  My script had the same problem.  When I tried to use my Edit option though the replacement edit work the save in my script did not work correctly. I have to look at your Template layer group to see what in there.  I need to find out why my script failed with your template.

The reason my Script's Edit option fails is because I want to be able to handle PNG files with transparent borders so I add a layer in overlay mode and add two Pixels 1% opacity in the top left and bottom right corners. I may be able to add a merger visible layers.  The Problem with your template for my script is your smart object is a Placed Jpeg file When I tried to save the edited object which I added layers to Photoshop would not save the object because the original object was a jpeg.

So I Edited your van gogh object duped the background and save it as a psd.  I then used replace content and replaced the jpeg object with the layered van gogh and saved the mockup template.   My script then works using its edit option with you images with the incorrect Print resolutions.

As I wrote Place is strange and has issues.  Replace content uses place and for some reasion the resize place was trying to do was eating up all your machines resources.  When the task manager showed me that Photoshop Rams utilization was over  27GB I killed Photoshop so it would not start to fill my boot disk for its scratch space.

Your object is 1125x750 with a print resolution setting of 300DPI You should preprocess you images of edit the object yo be able to use ant size image.  If you mat the original object from a Photoshop layer  the object work file will be created as a psb file by Photoshop.  Layers will be OK when you edit the object.

4 replies

piXelsplasher
Inspiring
November 12, 2019

There is now an extension which may not have this problem at Adobe Exchange for batch replacement of one or more smart layers with comp layer support:

Pixelsplasher Batch Smart Layer Replace 

This extension can replace multiple smart layers at every export and can auto-fit the new image to the original dimension of the replaced smart layer so there is no need to resize the source images.

FEATUREShttps://www.pixelsplasher.com/_downloads/scripts/batch-replace-smart-layer-content-script-for-adobe-photoshop/

Legend
April 10, 2019

Perhaps there is a corrupted file.
Try this version of the function

function replaceContents(newFile, theSO)

    {

    app.activeDocument.activeLayer = theSO;

    var d = new ActionDescriptor();

    d.putPath(stringIDToTypeID("null"), newFile);

    try {

        executeAction(stringIDToTypeID("placedLayerReplaceContents"), d, DialogModes.NO);

        }

    catch (e)

        {

        alert(newFile + "\n\n"+ "This file causes an error. Now will try to open it.");

        try {

            app.open(newFile);

            }

        catch (e)

            {

            alert(e);

            }

        executeAction(stringIDToTypeID("stop"), undefined, DialogModes.NO);

        }

   

    return app.activeDocument.activeLayer

    };

JJMack
Community Expert
Community Expert
May 1, 2019

I find in CC 2019 Photoshop a script that uses a replace content function that should work  will ocassionally faill with an error that the users cancelsd the operation. When this happens the Smart object layer the Replace conten was used on becomes broken and its content is not replace however other smart object layers that shared the object contants are updated.  So the Layer that shoule be sharing the object now have different content. It and intermuttent failure does not happen often.

Photoshop: Replace Content occasionally corrupt Photoshop active document | Photoshop Family Customer Community

JJMack
greless
Inspiring
April 10, 2019

i tried this script to work well in CC2017 and CC2019,so What a strange question you have

JJMack
Community Expert
Community Expert
April 10, 2019

The script they posted also works in CC 2018 howevet the script the have the error in is not that script the execute action is line 54 not 52 like his error.

JJMack
bufumofo
bufumofoAuthor
Participating Frequently
April 10, 2019

Correct - it has either something to do with the mockup file or my computer/Photoshop settings, as I've followed advice from another Adobe Forums member to use the script with a different mockup template and it worked just fine.

JJMack
Community Expert
Community Expert
April 10, 2019

Your script worked on my machine However none of the image I chose  were the correct size  so the jpeg image saved were not acceptable. Windows 10 CC 2019...

You need to know that replace content  Replaces the Smart Object Layser's object.  And it is like using  Photoshop Place feature up to a point.  Every Smart Object layers has and associated transform like Place has a transform. To positions and sizes te layers content.  However when you use  Layers>Smart Object>Replace Content only the Place part is actually done.  The Smat Object Layers associated transforms are not replaced or edited. All the existing transforms remains intact.   This means that the replacement object must have the exact same size, aspect ratio and print resolution as the existing  object for the current associated transforms to work correctly.  Because Place resizes image based on print resolutions.  The Smart Object rendered pixels must match the Aspect ratio and size of the current object.  You can not use any size image file to replace an smart object layers object.  If you want to use any size image file you may be able to open the Smart Object and edit its content and replace it with any size image after first resizing the image and fixing its aspect ratio.  You need to be careful there. The existing object needs to be a Photoshop object it can not be a RAW file or an Illustrator file those smart object will open in ACR and AI not Photoshop your script will not be able to control ACR or AI processing.

You may want to look at my script "BatchMockupTemplates.jsx" that is in  my Photo Collage and Mockup Toolkit. The Script can use Replace Content or Edit Smart object to update a documents top smart objects layers. For Details and Download use this link Documentation and Examples

JJMack
bufumofo
bufumofoAuthor
Participating Frequently
April 10, 2019

JJMack - thank you for your detailed response. Your man who provided me with the mockup template, said that if I'd use all 1250x750px images with the script, it should work just fine. He actually demonstrated it step by step in a video. I followed his every step and still get the errors so we can't comprehend. If the image files are all in the same image format, the aspect ratios and print resolutions would be too, right? I checked the smart object pixels and it matches 1250x750 pixels exactly.

I got the advice from another forum member today to use the script with a different mockup, so I downloaded one and to my surprise it worked flawlessly. It's just not the one I outsourced and desperately would like to use. I tried using your script ''BatchMockupTemplates.jsx'' script with it, but am getting an I/O error after about 10 mins of processing (for one image). Sometimes no image comes out, sometimes the attached comes out (while it obviously should have come out in 3D).

     

JJMack
Community Expert
Community Expert
April 10, 2019

Upload your mockup so I can download and try populating it.  I used that script to process 41 images into 84 Templates producing 3,444 jped files.  Three times I ran the Test Photoshop messes up one of the 84 templates and  it failed.  The test takes about 1.5 hours and Does not always fails and when it does fail it fails on the same template but not with the same image.  If fails in the Replace Content Path.  I have never has a failure when I used the Edit option where the Smart object is opened an replaced by editing the image. You could  try checking the edit option  see if it runs when replace content is not used.

Please upload your Mockup Template I would like to see if its a solid failure with that template.

Also give me some idea as to your Machine specs.  Photoshop is a resource hungry application and users report when processing thousands of images files Photoshop stops working for it wants more and it use all available resources. 

That script can run a long time before I add suspend history it ran very slow and consumed all my machines resources.

JJMack