0
votes

Just facing this issue: I have a mockup in Photoshop with two smart-objects: Rectangle 14.psb and Place your logo.psb I have 100+ images in png that should be applied to create mockups. For this reason, I would like your help to create a script that:

Let me select the png file that I would like to use

Open the smart objects (Rectangle 14.psb and Place your logo.psb)

Re-Link the same png to the layers "place your logo" of both the smart objects.

Finally, the script should save the file as png with the same file name of the selected png file adding just _new after its name.

So far I have tried this code without any luck:

#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;
// PSD Options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// 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;*.png", 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
            myDocument.saveAs((new File(thePath + "/" + theName + "_" + 
theNewName + ".psd")), psdOpts, true);
        }
    }
}
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|png|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
};

The above code substitute the smart object but I would like just to re-link the layer withing the smartobject to a new image and save the file. Any help would be much appreciated!

1
what do you mean by "just to re-link the layer"? you want you SO to become a linked SO to that png file? I think it'd be useful to have a screenshot of current result VS the expected result - Sergey Kritskiy

1 Answers

1
votes

Are you familiar with Scriptlistener? You can use it to get all the functions you need and then modify the output to run within your loop of 100 pngs, it should be straightforward.