如何将智能对象图层中心与画布对齐?

时间:2013-10-27 13:20:24

标签: javascript photoshop-script

我一直在谷歌搜索找到一种方法来将一个转换为智能对象中心的图层与脚本编写的画布对齐,但到目前为止还没有找到解决方案。我最终得到了下面的代码,但它没有完成这项工作。有人可以帮忙吗?

var baseFile = new File(openDialog()); //open base JPEG file
var workFile = new File(openDialog()); //open work JPEG file
var baseDoc = app.open(baseFile);
var workDoc = app.open(workFile);

createSO(workDoc.layers[0]);
workDoc.resizeImage(280,280);
workDoc.artLayers[0].duplicate(baseDoc, ElementPlacement.INSIDE);
app.activeDocument = baseDoc;

var Hoffset = (baseDoc.width - workDoc.width) / 2;
var Voffset = (baseDoc.height - workDoc.height) / 2;
baseDoc.layers[1].position = Array(Hoffset, Voffset);

function createSO(){
    var doc = app.activeDocument;
    var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
    executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
    return doc.activeLayer;
}

1 个答案:

答案 0 :(得分:0)

我认为你可以用这种方式简化:

var baseFile = new File(openDialog()); //open base JPEG file
var workFile = new File(openDialog()); //open work JPEG file
var baseDoc = app.open(baseFile);
var workDoc = app.open(workFile, undefined, true);

workDoc.resizeImage(280, 280);
var duplicated = workDoc.artLayers[0].duplicate(baseDoc, ElementPlacement.INSIDE);
var Hoffset = (baseDoc.width - workDoc.width) / 2;
var Voffset = (baseDoc.height - workDoc.height) / 2;

app.activeDocument = baseDoc;

duplicated.translate(Hoffset, Voffset);

这里的要点是使用translate方法;但是你也可以使用createSO的第三个参数(即app.open)来避免创建asSmartObject函数。