Photoshop script how to add image to a layer

wazzaday picture wazzaday · Jun 24, 2015 · Viewed 7.4k times · Source

How can I grab an image by its location on my local machine and insert it into a layer with 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

All I can seem to do is open the image as a background which creates a new photoshop doc in the process;

var opened = open(img);

But what I would like to achieve is to open multiple images into the same doc as multiple layers. Can this be done?

Answer

Anna Forrest picture Anna Forrest · Jun 25, 2015

Open each of the images you want to consolidate using the open method you found. Then cycle through the open document and use the duplicate method on the art layer object to copy all the layers to a single target document. See the code snippet below for copying a single layer image to a new doc.

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