最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSX duplicate layer Adobe Photoshop - Stack Overflow

programmeradmin1浏览0评论

I have a problem with duplicating layers from one document to another. I have this code (.jsx script inside my Photoshop document)

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)
}

But I am still receiving an error

Error: You can only duplicate layers from the frontmost document.

Have you any ideas how to make it work?

I have a problem with duplicating layers from one document to another. I have this code (.jsx script inside my Photoshop document)

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)
}

But I am still receiving an error

Error: You can only duplicate layers from the frontmost document.

Have you any ideas how to make it work?

Share Improve this question edited Jun 29, 2019 at 16:19 Mark Amery 156k90 gold badges430 silver badges472 bronze badges asked Dec 23, 2012 at 15:01 yetyyety 7214 gold badges16 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

The reason you're getting an error is dupObj is never defined. I think you mean to use docRef, the reference to your source document in line 1. This seems to work fine now:

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)
}

That being said there might be a few hidden bugs in there you should look at. In this line:

docRef.artLayers[i].duplicate(document, ElementPlacement.INSIDE);

i is never defined, and apparently defaults to 0 without throwing an error. The result is you will only ever duplicate the first layer in the artLayers array.

Also, since you are selecting the entire document using app.activeDocument.selection.selectAll(); there is no need to calculate the size of the selection. It will always be the same size as the original document. You could just use docRef.width and docRef.height as the width and height for the new document. Besides, when you duplicate a layer it will copy the whole layer regardless of the selection, as far as I know.

If you just want to make a new document the same size as the layer you are duplicating try using artLayers[i].bounds instead of selection.bounds

You're not calling the active document: You need to call a reference to the active document and the one your using - hence the error.

var docRef = app.activeDocument;
docRef.selection.selectAll();

var calcWidth  = docRef.selection.bounds[2] -app.activeDocument.selection.bounds[0];

var calcHeight = docRef.selection.bounds[3] - app.activeDocument.selection.bounds[1];
var docResolution = docRef.resolution;

var document = app.documents.add(calcWidth, calcHeight, docResolution);
app.activeDocument = docRef;

try {
  dupObj.artLayers[i].duplicate(document, ElementPlacement.INSIDE);
}
catch(e) {
  alert(e)
}

I've not used dupObj before as I use CS and script listener code for duplicating documents And I've not checked the code, but give it a go.

The problem is that you're trying to use a variable called document which is reserved in JS.

As Sergey pointed out, document is (amazingly) not a reserved word in JSX because Adobe JSX is not 'regular' JSX

Although it doesn't address the exact syntax error I'll leave this here because it's a quick way to solve the overall problem of copying layers between documents.

// Grab docs
const doc1 = app.activeDocument
const doc2 = app.documents.add(100, 100)

const outputLayer = doc1.layers[0]
const inputLayer = doc2.layers[0]

inputLayer.duplicate(outputLayer, ElementPlacement.INSIDE)
发布评论

评论列表(0)

  1. 暂无评论