• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

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 , Windows

Views

956

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

Valorous Hero , May 01, 2021 May 01, 2021

Try

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

Votes

Translate

Translate
Adobe
Community Expert ,
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

Votes

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);"

Votes

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
Valorous Hero ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Try

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

Votes

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.

Votes

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("\","\\");

Votes

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
Valorous Hero ,
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.

Votes

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