Skip to content

Instantly share code, notes, and snippets.

@laryn
Last active October 29, 2024 20:36
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 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!

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