Photoshop脚本如何将图像添加到图层

时间:2015-06-24 12:40:58

标签: photoshop extendscript

如何通过本地计算机上的位置抓取图像并将其插入带有extendscript的图层?

var doc = app.documents.add();
var layer = doc.artLayers.add();
var img = new File('~/Desktop/tanks.png'); 
layer.image = img; //I want to add the image to this layer

我所能做的就是打开图像作为背景,在此过程中创建一个新的photoshop文档;

var opened = open(img);

但我想要实现的是将多个图像打开到同一个文档中作为多个图层。可以这样做吗?

2 个答案:

答案 0 :(得分:3)

使用您找到的打开方法打开要合并的每个图像。然后循环打开文档并使用艺术图层对象上的复制方法将所有图层复制到单个目标文档。请参阅下面的代码段,将单层图像复制到新文档。

    //copy the layer into the target document
    app.activeDocument = pSourceDocument;
    pSourceDocument.artLayers[0].duplicate(pTargetDocument); 

答案 1 :(得分:1)

我在这里找到了一个非常有用的脚本https://forums.adobe.com/message/3946944#3946944

我拿了这个脚本,它对我有用。首先,您需要将要替换的内容层与图像转换为智能对象(在其他情况下,图层的内容不能被脚本替换)。要执行此操作,请在Photoshop中打开要修改的文件,选择图层,然后单击图层>智能对象>集成新的智能对象。现在这个图层是一个智能对象。

然后使用以下代码创建一个脚本:

var replacementFile = new File("X:/file.jpg");
var theLayer = app.documents[0].artLayers.getByName("targetLayer");
theLayer = replaceContents(replacementFile);

////// replace contents //////  
function replaceContents (newFile) {  
// =======================================================  
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );  
    var desc3 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
    desc3.putPath( idnull, new File( newFile ) );  
    var idPgNm = charIDToTypeID( "PgNm" );  
    desc3.putInteger( idPgNm, 1 );  
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );  
return app.activeDocument.activeLayer  
};