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

Engaged ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Hello everyone,

I have a document with a smart object on it:

AG_Ps_100_2-1619872849638.png

 

 

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

 

TOPICS
Actions and scripting, How to, Windows

Views

351

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 01, 2021 May 01, 2021
Try "E:\\REPLACE SO\\DSC_4591_Round.jpg"

Likes

Translate

Translate
LEGEND ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Try

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

@r-binhow do I replace the slashes?

 

doesn't work:

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

LATEST

I didn't understand what you want.

You have a syntax error here, an incomplete string.

Two slashes (in a string, in a code) this is one in reality. It's just that in the code, just a single slash is used to denote special characters.

 

Use single "/" instead of "\\" in the code for paths.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines