使用Photoshop批量替换智能对象

时间:2018-10-03 16:08:21

标签: photoshop photoshop-script

仅遇到此问题:我在Photoshop中有一个带有两个智能对象的模型:Rectangle 14.psb和Place your logo.psb 我在png中有100多个图像,应该将其应用于创建模型。 因此,我希望您的帮助来创建一个脚本:

让我选择我要使用的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 个答案:

答案 0 :(得分:1)

您熟悉Scriptlistener吗?您可以使用它来获取所需的所有功能,然后修改输出以在100 png的循环内运行,这应该很简单。

相关问题