Skip to main content
AG_Ps_100
Inspiring
May 1, 2021
Answered

Photoshop Script - Replace Smart Object from path (no dialog)

  • May 1, 2021
  • 2 replies
  • 1946 views

Hello everyone,

I have a document with a smart object on it:

 

 

I have this image path: "E:\REPLACE SO\DSC_4591_Round.jpg"

 

I want a script to replace the smart object with the image path i have without using a dialog to select the file, is that possible?

 

This is what I've tried and it doesn't work:

var mySmartObjectLayer = activeDocument.artLayers.getByName("My Smart Object");

var PathFile = "E:\REPLACE SO\DSC_4591_Round.jpg";

var myFile = File(PathFile);

replaceContents(myFile, mySmartObjectLayer);

function replaceContents(newFile, theSO) {
    app.activeDocument.activeLayer = theSO;
    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
};

 

This topic has been closed for replies.
Correct answer r-bin

Try

"E:\\REPLACE SO\\DSC_4591_Round.jpg"

2 replies

r-binCorrect answer
Legend
May 1, 2021

Try

"E:\\REPLACE SO\\DSC_4591_Round.jpg"

AG_Ps_100
AG_Ps_100Author
Inspiring
May 1, 2021

Works. r-bin you're the best as usual.

AG_Ps_100
AG_Ps_100Author
Inspiring
May 1, 2021

@r-binhow do I replace the slashes?

 

doesn't work:

var PathFile = PathFile.replace("\","\\");

JJMack
Community Expert
Community Expert
May 1, 2021

You may as well do that manually that  if your image "E:\REPLACE SO\DSC_4591_Round.jpg" is not the correct aspect ratio size and print resolution  for the object in your smart object layer named "My Smart Object"  the layers object transform will not be correct and you will need to rectify the issue. The code may work but the result may not be what you want. All smart object layers have a recorded transform for the layers object for generating the layer pixels content from the smart object.  If the replacement Image has the corrects Aspect ratio, size and resolution. If the core is correct the replacement should work correctly.  Your code the statement

var myFile = File(PathFile);

may need to be 

var myFile = New File(PathFile);

I only hack at scripting Photoshop  I do not really know Photoshop scripting well.  My hacking skill are OK.

JJMack
AG_Ps_100
AG_Ps_100Author
Inspiring
May 1, 2021

Thank you JJ,
I totally forgot about "var myFile = New File(PathFile);"