Skip to main content
February 21, 2023
Question

Photoshop - replace multiple smart objects - find by name

  • February 21, 2023
  • 1 reply
  • 5062 views

Hi Adobe support community,

Is there a way to replace multiple Smart-objects in Photoshop by selecting one map  on my harddisk (with all the files that needs to be replaced)

Is It possible that Photoshop can find then the same name as in the Smart object and replace the files by name. So multiple Smart-objects need to be replaced with new images in one map on my computer. I Want the script does a find and replace thing.

I hope someone can help me, now I replace all the smart images one-by-one. 

- Select smart object - Replace content - search for the right name and so on for 10 times sometimes in a photoshop file. It is very time-consuming.

I hope someone can help me, you can also send me a message here.
The Screenshot shows all my layers that need te replaced.

For further questions please don't hesitate to send me a message.

Kind regards,

Eline Kouijzer


This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
February 21, 2023

You seem to be using the term »map« repeatedly – do you mean a »Folder«? 

 

Could you please post a screenshot of both the image’s Layers Panel and the replacement files in the File Explorer? 

Do the Smart Objects have multiple instances in the document? (Avoiding double-replacing would seem prodent to save time.)

February 21, 2023

Hi, Thanks for your reply.
Attached an screenshot of my Folder on the computer. The tiff files need to be replaced in the photoshop. Left is the folder and right are the photoshop smart objects that needs to be replaced. 

Some of the smart objects are instances, but I cannot use an instance for reflection.tiff and for Specular.tiff. These are two different files. 


c.pfaffenbichler
Community Expert
February 21, 2023

"I want to know what the exact relative positions of the folders containing the psb and the tiff-files are. "

 

See the attached screenshot.

PSB: 
"E:\3DS-Max\PHOTOSHOP\Cam_05\Cam_05.psb"

TIFF(Render passes): 
"D:\RENDERS\Cam_05\Cam05_V05.VRayReflection.0000.tif"
"D:\RENDERS\Cam_05\Cam05_V05.VRaySpecular.0000.tif"

I don't know if it is better to work with UNC-names now? 


You can give this a try, you need to amend theTifFolder, though, to the position on your D-Drive. 

In my test with this Folder-structure 

I get these updates:

// replace smart objects with tif with number plus one;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
// get renderings folder;
if (myDocument.name.indexOf(".") != -1) {var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]}
else {var myDocName = myDocument.name};
try {var myPath = myDocument.path}
catch (e) {var myPath = "~/Desktop"};
var theTifFolder = Folder(Folder(myPath).parent+"/renderings");
// collect smart objects;
var theSmartObjects = collectSmartObjects2017();
var theRegExp = /_Cam\d{2}\.VRay/i;
// work through smart objects;
for (var m = 0; m < theSmartObjects.length; m++) {
    var theMatch = theSmartObjects[m][0].match(theRegExp);
    if (theMatch != null) {
        var theReplace = theSmartObjects[m][0].replace(theRegExp, "_Cam"+bufferNumberWithZeros(Number(String(theMatch).match(/\d{2}/))+1, 2)+".VRay");
        if (File(theTifFolder+"/"+theReplace+".tif").exists == true) {
// replace smart object it matching tif exists;
            replaceContents (File(theTifFolder+"/"+theReplace+".tif"), theSmartObjects[m][1]);
        };
    }
};
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjects2017 () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {theLayers.push([theName, theID])}
};
}
catch (e) {};
};
return theLayers
};
////// replace contents //////
function replaceContents (newFile, theSOId) {
selectLayerByID(theSOId, false);
// =======================================================
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
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// buffer number with zeros //////
function bufferNumberWithZeros (number, places) {
	var theNumberString = String(number);
	for (var o = 0; o < (places - String(number).length); o++) {
		theNumberString = String("0" + theNumberString)
		};
	return theNumberString
	};