使用 Photoshop 批量替换智能对象

问题描述 投票:0回答:2

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

让我选择我想要使用的 png 文件

打开智能对象(矩形 14.psb 和放置您的徽标.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
};

上面的代码替换了智能对象,但我只想将智能对象所在的图层重新链接到新图像并保存文件。任何帮助将不胜感激!

photoshop photoshop-script
2个回答
1
投票

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


0
投票

我能够解决这个问题。有几种主要方法可以在 Photoshop 内自动创建模型图像。

  1. 您可以使用一个简单的 Photoshop 插件来自动执行此过程,例如批量替换智能对象。这基本上会从源文件夹中获取所有图像,用每个图像替换智能对象内容,然后将每个更新的图像导出到输出文件夹。如果您没有编写 Photoshop 脚本/.jsx 代码的经验,这是实现此目标的最简单选择。

  2. 使用 Photoshop 脚本。正如 Sergey 建议的那样,使用“脚本侦听器”工具,例如 Adobe 描述的thisone。这可以让您运行所需的操作(例如,用指定的图像替换智能对象),然后它将记录执行的 Photoshop 命令。然后,您可以将这些 Photoshop 友好的命令转换为执行指定操作所需的实际 .jsx 代码行。

  3. 了解如何执行此操作的另一种方法是查阅 Adobe 脚本参考指南。 Github 上的这个 是帮助您入门的有用资源。这将帮助您了解如何实际编写这些 Photoshop 脚本来执行所需的操作(而不是尝试从脚本侦听器中复制+粘贴内容并希望它能正常工作。)

© www.soinside.com 2019 - 2024. All rights reserved.