-
-
Save laryn/0a1f6bf0dab5b713395a835f9bfa805c to your computer and use it in GitHub Desktop.
// 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 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...
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?
@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!
@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