skip to Main Content

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?



  1. 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;
    Login or Signup to reply.
  2. I found a very useful script for doing this here

    I took the piece of this script and it worked for me. First of all, you need to convert the layer which contents you want to replace with the image to a Smart Object (in other case contents of the layer can’t be replaced by scripts). To do so, open the file you want to modificate in Photoshop, select the layer, click Layer > Smart Objects > Group into New Smart Object. Now this layer is a Smart Object.

    Then create a script with the following code:

    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  
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top