Photoshop script how to add image to a layer

11.5k views Asked by At

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?

4

There are 4 answers

0
Anna Forrest On BEST ANSWER

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); 
0
Anuj Mahur On

if the opened image is vertical the insert it in another vertical PSD file layer else insert images is horizontal then insert it into a horizontal PSD file layer javascript for photoshop

1
king_anton On

This is a very common operation in Photoshop. Typically it's done by creating a Smart Object layer. You can then replace the Smart Object with the image that you want. (This is often used when creating mockup images, for example -- you have a PSD template document, with a placeholder image that gets replaced with the artwork or design of your choice.) See this Adobe guide on Smart Objects to learn more about how they work.

If you're doing a high volume of this work, I would point out that this process can be easily automated inside of Photoshop. So let's say you're creating a ton of mockup images for an online store, for example. Instead of manually replacing the Smart Object, and exporting the final image, one at a time, you can automate this process in Photoshop using one of two methods:

METHOD 1) Photoshop scripting. This is basically code that you write, and execute inside of Photoshop, to run the specified commands automatically. It's not easy to write from scratch, however there is a helpful Adobe guide on scripting that can help you get started. Additionally, there is a Scripting Listener tool you can use inside of Photoshop, where you can basically perform the actions you want to perform, and it will log the scripting code that executes these commands to a file. You can then convert that logged code to the .jsx code you'll use to automate this process via Photoshop scripting. If you need help with this, the Adobe Community forums have a lot of posts focused on troubleshooting Photoshop scripts and getting them to work properly.

METHOD 2) Use a Photoshop plugin. This is the simpler solution, which doesn't require any computer programming experience. There's a plugin which allows you to automate this process, called Batch-Replace Smart Objects. So if you have a high volume of images in your input folder, you can automate the replacement of the Smart Objects + the export of the final images. If you have multiple steps in the workflow (maybe you're using multiple PSD documents to create several mockup images), you can set up multiple step workflows using this plugin as well. Then just click a button, hit run, and it will automate this batch replacement of the Smart Objects for you.

0
Helen On

I found a very useful script for doing this here https://forums.adobe.com/message/3946944#3946944

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  
};