Search code examples
javascriptphotoshopphotoshop-script

Replace Layer image in Photoshop scripting


How can I replace the image in a layer? For a text layer I can easily do doc.layerSets.getByName('title').textItem.contents = 'New Text'. So my question is, is there something similar to textItem that works with images. something like imageItem. so I can replace the image in a layer?


Solution

  • If I understood you correctly, you want to copy all image layers' contents from a group into a single target layer.

    You could accomplish this with the intuitive way of iterating through the group art layers and in-place copying/pasting their contents to the target layer. This might seen trivial in the GUI, but programmatically, it's way too complicated.

    Another way, and perhaps a more programmatic way, would be to just iterate through the group art layers, duplicate each layer and merge them with the target layer. I'll give you an example:

    var doc = app.activeDocument;
    var target = doc.layers.getByName('target');
    var group = doc.layerSets.getByName('images');
    var gal = group.artLayers;
    
    // Iterate through art layers from group "images"
    for (var i = 0; i < gal.length; i++) {
      // Duplicate current layer and put it before the target layer
      var temp = gal[i].duplicate(target, ElementPlacement.PLACEBEFORE);
    
      // Merge the current layer with the target
      // (the target variable has to be updated because it's now a new merged layer)
      target = temp.merge();
    }
    

    This works fine with a simple art target layer. Now if your target layer is more complex (i.e. it has blending properties, masks and etc.) and you want to maintain that, you'll have to resort to the "paste in place" method linked above.