使用 Photoshop 扩展脚本:导入图像

Extendscript with Photoshop: Importing an image

我正在尝试通过提示 select 一些图像文件,然后将这些文件添加到活动文档中。这是我目前拥有的:

#target photoshop

doc = app.activeDocument;

// choose image files
var files = File.openDialog(undefined,undefined,true);

// for each image, add to new layer and insert into doc
for (var file in files) {
    var layer = doc.artLayers.add();
    layer.image = file; // this doesn't work.
}

什么是 layer.imageArtLayer 没有这个 属性。也许放置在您的情况下会更好:

doc = app.activeDocument;

// choose image files
var files = File.openDialog(undefined, undefined, true);

// for each image, add to new layer and insert into doc
for (var i = 0; i < files.length; i++)
{
    var layer = placeImage(files[i]);
}

function placeImage(imageFile)
{
    var desc554 = new ActionDescriptor();
    desc554.putPath(cTID('null'), imageFile);
    desc554.putEnumerated(cTID('FTcs'), cTID('QCSt'), cTID('Qcsa'));
    var desc555 = new ActionDescriptor();
    desc555.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), 0.000000);
    desc555.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), 0.000000);
    desc554.putObject(cTID('Ofst'), cTID('Ofst'), desc555);
    executeAction(cTID('Plc '), desc554, DialogModes.NO);

    return activeDocument.activeLayer
};

function cTID(s)
{
    return app.charIDToTypeID(s);
};

function sTID(s)
{
    return app.stringIDToTypeID(s);
};

此外,Photoshop 不喜欢在数组上使用 for...in 循环(有时有效,有时无效:在 File 对象的情况下无效)