以下代码的原始版本通过提示用户输入新图像来替换智能对象的内容。我尝试修改它,以便它也替换嵌套智能对象的内容。
我不是编码员,通常通过引用现有代码并将不同的代码混合在一起并在谷歌中搜索指针来进行管理。然而,我尝试过的所有操作仍然忽略嵌套的智能对象。
下面是我尝试包含嵌套智能对象的尝试之一,但是替换智能对象内容的核心功能已被破坏,现在它什么也不做。如何实现包含嵌套智能对象?
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
// Call the function to start the replacement process
replaceAllSmartObjectContents(myDocument);
}
function replaceAllSmartObjectContents(currentLayer) {
if (currentLayer.typename === "ArtLayer" && currentLayer.kind === LayerKind.SMARTOBJECT) {
// If it's a Smart Object layer, replace its contents
replaceSmartObjectContents(currentLayer);
} else if (currentLayer.typename === "LayerSet") {
// If it's a layer group, recursively check its child layers
for (var i = 0; i < currentLayer.layers.length; i++) {
replaceAllSmartObjectContents(currentLayer.layers[i]);
}
}
}
function replaceSmartObjectContents(smartObjectLayer) {
// Select the Smart Object layer
app.activeDocument.activeLayer = smartObjectLayer;
// Select Files
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("Please select files", "*.psd;*.tif;*.jpg", true);
} else {
var theFiles = File.openDialog("Please select files", getFiles, true);
}
if (theFiles) {
for (var m = 0; m < theFiles.length; m++) {
// Replace Smart Object
smartObjectLayer = replaceContents(theFiles[m], smartObjectLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
}
}
}
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
return true;
}
}
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;
}
这里的问题是您的
replaceAllSmartObjectContents
函数具有以下签名:
function replaceAllSmartObjectContents(currentLayer) {
...
}
所以你的函数需要一个层。但是,您使用以下方式调用它:
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
// Call the function to start the replacement process
replaceAllSmartObjectContents(myDocument);
}
我们可以看到您正在向其传递文档,而不是图层。显然,这是行不通的。如果我们将其更改为:
replaceAllSmartObjectContents(myDocument.activeLayer);
并且您的脚本工作正常,前提是当然,我们有一个具有适当名称的图层。