Skip to main content
Participant
July 29, 2022
Question

Need to replace an image with an image from a URL using extendscript

  • July 29, 2022
  • 3 replies
  • 484 views

I am making a script to automatically generate a thumbnail by opening a template PSD and editing the contents based on data from an API, and one of those contents is a background image. I can't find a way to get an image from a URL and replace the contents of a layer with said image. The closest thing I've come across is this script

function replaceContents (newFile) {  
    // =======================================================  
    var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );  
        var desc5 = new ActionDescriptor();  
        var idnull = charIDToTypeID( "null" );  
        desc5.putPath( idnull, new File( newFile ) );  
    executeAction( idplacedLayerReplaceContents, desc5, DialogModes.NO );  
    return app.activeDocument.activeLayer
};

which works with smart objects, but it gives me an error when I try and convert a layer to a smart object like this:

whateverLayer.kind = LayerKind.SMARTOBJECT

 

I need either a way to convert the layer to a smart object, or just a totally different and easier way to replace a layer with an image.

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
August 6, 2022

@LiamR36 wrote:

but it gives me an error when I try and convert a layer to a smart object like this:

whateverLayer.kind = LayerKind.SMARTOBJECT

 

As you have found, that isn't valid code.

 

To convert an active layer to a smart object:

 

// Convert layer to smart object
var convertToSmartObject = stringIDToTypeID("newPlacedLayer");
executeAction( convertToSmartObject, undefined, DialogModes.NO );

 

Or perhaps:

 

// Convert layer to smart object
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
app.displayDialogs = savedDisplayDialogs;

 

Legend
August 1, 2022

Just delete the layer, create a new one, and paste in the image.

c.pfaffenbichler
Community Expert
Community Expert
August 1, 2022

This would convert the active Layer/s to one Smart Object. 

////// convert to smart object //////
function convertToSmartObject () {
var desc108 = new ActionDescriptor();
var ref77 = new ActionReference();
ref77.putEnumerated( charIDToTypeID( "Mn  " ), charIDToTypeID( "MnIt" ), stringIDToTypeID( "newPlacedLayer" ) );
desc108.putReference( charIDToTypeID( "null" ), ref77 );
executeAction( charIDToTypeID( "slct" ), desc108, DialogModes.NO );
};
LiamR36Author
Participant
August 6, 2022

do the variable names ref77 and desc108 matter? I want to learn what a lot of this does as well.