问题
I've been googling all day to find a way to align a layer which is converted to Smart Object center to the canvas proceeded by scripting, but haven't found a solution so far. I ended up with the code below, but it doesn't do the job. Could anyone help, please?
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:
I think you can simplify a bit in this way:
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);
The main point here is using translate
method; however you can also avoid to create a createSO
function, using the 3rd argument of app.open
(that is asSmartObject
).
来源:https://stackoverflow.com/questions/19618505/how-to-align-smart-object-layer-center-to-the-canvas