我在将图层从一个文档复制到另一个文档时遇到问题。我有这个代码(.jsx 脚本在我的 Photoshop 文档中)
var docRef = app.activeDocument;
app.activeDocument.selection.selectAll();
var calcWidth = app.activeDocument.selection.bounds[2] -app.activeDocument.selection.bounds[0];
var calcHeight = app.activeDocument.selection.bounds[3] - app.activeDocument.selection.bounds[1];
var docResolution = app.activeDocument.resolution;
var document = app.documents.add(calcWidth, calcHeight, docResolution);
app.activeDocument = docRef;
try {
dupObj.artLayers[i].duplicate(document, ElementPlacement.INSIDE);
}
catch(e) {
alert(e)
}
但我仍然收到错误
Error: You can only duplicate layers from the frontmost document.
你有什么想法让它发挥作用吗?
最佳答案
您收到错误的原因是 dupObj
从未定义。我认为您的意思是使用 docRef
,即第 1 行中对源文档的引用。现在这似乎工作正常:
var docRef = app.activeDocument;
app.activeDocument.selection.selectAll();
var calcWidth = app.activeDocument.selection.bounds[2] -app.activeDocument.selection.bounds[0];
var calcHeight = app.activeDocument.selection.bounds[3] - app.activeDocument.selection.bounds[1];
var docResolution = app.activeDocument.resolution;
var document = app.documents.add(calcWidth, calcHeight, docResolution);
app.activeDocument = docRef;
try {
docRef.artLayers[i].duplicate(document, ElementPlacement.INSIDE); // ** changed to docRef **
}
catch(e) {
alert(e)
}
话虽这么说,其中可能存在一些隐藏的错误,您应该注意一下。在这一行中:
docRef.artLayers[i].duplicate(document, ElementPlacement.INSIDE);
i
从未被定义,并且显然默认为 0 且不会引发错误。结果是您只能复制 artLayers 数组中的第一层。
此外,由于您使用 app.activeDocument.selection.selectAll();
选择整个文档,因此无需计算所选内容的大小。它始终与原始文档的尺寸相同。您可以仅使用 docRef.width 和 docRef.height 作为新文档的宽度和高度。此外,据我所知,当您复制图层时,无论选择如何,它都会复制整个图层。
如果您只想创建一个与要复制的图层大小相同的新文档,请尝试使用 artLayers[i].bounds
而不是 selection.bounds
关于javascript - JSX 复制图层 Adobe Photoshop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14011967/