Answered
How to use a script to find smart objects by name and replace images in that smart object
- April 22, 2024
- 2 replies
- 911 views
I tried the code below, it works but I want to hide the old layers in the smart object to avoid having 2 images overlap like in the photo (Layer smart object name "sm3" is overlapping 2 images and I want to delete it or hide old photos)
// Set the path to the input folder
var inputFolderPath = "C:\\Users\\Admin\\Downloads\\avt";
// 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 <= 3; i++) {
var smartObjName = "sm" + i;
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 + "/a (" + i + ").png";
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;
};
