Copy link to clipboard
Copied
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
};
Try
"E:\\REPLACE SO\\DSC_4591_Round.jpg"
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.
Copy link to clipboard
Copied
Thank you JJ,
I totally forgot about "var myFile = New File(PathFile);"
Copy link to clipboard
Copied
Try
"E:\\REPLACE SO\\DSC_4591_Round.jpg"
Copy link to clipboard
Copied
Works. r-bin you're the best as usual.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.