Skip to content

Instantly share code, notes, and snippets.

@laryn
Last active March 17, 2025 08:51
Show Gist options
  • Save laryn/0a1f6bf0dab5b713395a835f9bfa805c to your computer and use it in GitHub Desktop.
Save laryn/0a1f6bf0dab5b713395a835f9bfa805c to your computer and use it in GitHub Desktop.
Photoshop script to replace a smart object within a PSD with selected image(s) and save result as a JPG. (Transformations to the smart object will be applied to new images).
// Replace SmartObject’s Content and Save as JPG
// 2017, use it at your own risk
// Via @Circle B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photoshops-variable-data-or-scripts/93359
// 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[m], theLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
// Save JPG. If you have need of saving different artboards into separate files,
// see https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c#gistcomment-3996733
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
};
@schroef
Copy link

schroef commented Dec 2, 2023

@milqnomadu
If you go to my GitHub link, I added it a couple posts above, it will show a layout in the layer panel. This is need to make it scale down the image to the mask dimensions. This will fix the issue I saw in that post you linked. I I understand correctly, your new linked image "design" is to big for the mockup, is that correct?

Edit
Here's the link to my repo
https://github.com/schroef/Photoshop-Scripts

Notice the layer panel and the order, this is very important for the resizing to work properly

@schroef
Copy link

schroef commented Jan 12, 2024

Ive been working on a new version of this script. After someone contacted me about a small issue with the current script. The original script did not resize the image. By adding the code from @magicsun and adding a mask, that work. But the image aspect ratio can cause an issue in that version. If the new images don't fit in terms of the image ratio, you would get blank spots. Ive also now have a version which replaces the content inside the linked SmartObject. This is a different approach and in some cases solves the issue.
I've also found a function on Adobe Community by Stephen A Marsh which allows the user to save as WebP.

I'm stilling testing out if this script can be one version or perhaps we keep both methods. Anyway, im working on adding a dialog window. The user can define settings and placement of the new image. This is for the version which edit the content of the SmartObject. The script also uses ObjectToDescriptor, this means when you reload the script it restores all last settings.

This is how it looks now, it already works with hard-coded save to WebP. Im now in the process of adding all other formats; BMP,JPG,PDF,PSD,TARGA,TIFF,WEBP,PNG-8,PNG-24.

dialog-design-v002

@shohidur333
Copy link

To replace a smart object within a PSD with selected image(s) and save the result as a JPG using a Photoshop script, you can refer to the detailed instructions provided in the article on BulkMockup. The article covers the use of scripts to automate the process and includes steps for replacing smart objects in bulk. The script mentioned in the article applies transformations to the smart objects and ensures that the changes are saved as JPG files.

@schroef
Copy link

schroef commented Jan 31, 2024

@shohidur333
My new versions actually does this, I've implemented 2 version now. One which edit the link itself, the other will edit the content inside the smartobject. The latter is slower however to the workflow need. But it's still a fraction of doing this all manually .

It's nice you have such expanded tutorials and writing. But it's a commercial product. $15 per month. Kinda steep price if you can get ir for free here

@vkpajeet
Copy link

The script is indeed effective, but it does have limitations when dealing with various template and art file scenarios. For those seeking a more comprehensive solution, the Bulk Mockup plugin (a paid tool) offers enhanced functionality:

  1. Multiple smart object layer replacement
  2. Design file alignment using a 3x3 matrix (9 anchor points)
  3. Nested smart object layer replacement
  4. Animated and video file export capabilities
  5. Customizable file naming for exports
  6. Flexible organization of exported files into folders and subfolders

While the original script works well for basic needs, Bulk Mockup addresses its limitations and provides greater versatility for complex projects.

@schroef
Copy link

schroef commented Oct 29, 2024

But... its free and not hidden behind a pay-wall! Even weirder, a monthly subscription for a script?!?!? mind-bogling!

@nasseroth
Copy link

Thank you very much friend, I was looking for this for a long time. Still working with Adobe Photoshop 2024.

@schroef
Copy link

schroef commented Jan 9, 2025

@nasseroth

Let me know if you run into issues. I have been working on other versions of this script with more options. I can't remember now how far I got with that one. It has different methods for feplace so it shows better in the template. I need to check the code and see how and if it works properly. Has been months since I worked on it.

@jordanash98
Copy link

Hey,

This is solid and works fine! Is there any chance we could get it to save as a PDF? All new to this so i just run into error after error when editing myself, thanks!

@schroef
Copy link

schroef commented Feb 7, 2025

@jordanash98

I have other versions. I've added a panel to it and different same methods. I'm not sure I've added all save options already though. I work on these scripts on and off. I adjust them as I need them at that moment.

I need to look into it

@petergambos
Copy link

@schroef Hi! Thanks a lot for the script! Been using it for a while, today I got this error:

Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

  • Could not complete the command because the result would be too big.
    Line: 100
    -> executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

Could you help with this, please? I was not able to find anything useful on the net how to troubleshoot it...

@schroef
Copy link

schroef commented Mar 14, 2025

@petergambos

Sure. Is it possible you send me an possible simplified version if the pad file. Its kind hard to say what's going on.

There is neither something with that linked file or something else

Ps which version of the script are you using, are you suing one from my GitHub, which is a couple posts above?

@petergambos
Copy link

@schroef sure, I uploaded a .zip file with a simplified .psd and few of the .jpg files that I want to batch replace with the script here:
https://drive.google.com/file/d/1P6gkPsgf4GSwR3uGhHLj4jLzcET94o5N/view?usp=sharing

I am using the "replace_smartobject_image-v004.js", so I suppose that is the latest version.

In the meantime, I was able to find some workaround — it seems that the problem is with the different DPI of the smart object and the replaced files. The smart object is set to have 1920x1080 with 300 DPI and the replaced files are 1920x1080 with 72 DPI. I supposed that the script can handle the resizing also with different DPI, but probably not. I created a simple action to bulk change all the replaced files to 300 DPI and then used the script (which worked), but if there is a possibility to improve the script to handle not only different dimensions, but also DPI, it would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment