提问者:小点点

用Photoshop批量替换智能对象


面对这个问题:我在Photoshop中有一个模型,有两个智能对象:矩形14。psb并放置您的徽标。psb我有100个png图像,应该用于创建实体模型。因此,我希望您帮助创建一个脚本:

让我选择要使用的png文件

打开智能对象(矩形14.psb并放置logo.psb)

将相同的png重新链接到两个智能对象的图层“放置您的徽标”。

最后,脚本应该将文件保存为png,文件名与选定的png文件相同,仅在其名称后添加_new。

到目前为止,我已经尝试了这段代码,但没有任何运气:

#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
};

上面的代码替代了智能对象,但我想只是重新链接层使用智能对象到一个新的图像和保存文件。任何帮助将不胜感激!


共1个答案

匿名用户

你熟悉Scriptlistener吗?您可以使用它来获取所有您需要的函数,然后修改输出以在100 pngs的循环中运行,这应该很简单。