0
votes

I have a PSD Web Template with multiple PSD files and I need to replace a keyword in all of them. If I use the built-in Find & Replace it will only replace one PSD file at a time, plus it's not replacing text inside Smart Objects.

Is there any such script that can help replace a keyword from everywhere (including Smart Objects) in a PSD (also in multiple PSDs in a folder)?

The closest I found here was at Find and replace text in multiple Photoshop files?

var dir = new Folder('/c/temp')
var files = dir.getFiles("*.psd");

for (var i = 0; i < files.length; i++) {
    var doc = app.open(files[i]);

    for (var j= 0; j < doc.artLayers.length; j++) {
        var lyr = doc.artLayers[j];

        if (lyr.kind == LayerKind.TEXT) {
            var lyr = doc.artLayers[j];
            lyr.textItem.contents = lyr.textItem.contents.replace("search","replace"); 
        }
     }

    doc.close(SaveOptions.SAVECHANGES)
}

But for some reason the script opens the PSD file and then closes it without making any changes. I tried to play the script one line at a time and it goes to line 10 (if (lyr.kind == LayerKind.TEXT) {) and loops back to line 7 (for (var j= 0; j < doc.artLayers.length; j++) {).

1
are you running that code on a document with any text layers that aren't in a smart object?? That code won't edit a smart object. - Anna Forrest
yes the psd i am trying contains text in and out of smart object and the text doesn't replace any of it. Not sure. - Sam Rizzi
Put a break point at the lyr.kind == LayerKind.TEXT line and see what type lyr.kind is returning when 'lyr' is one of the layers you think is a text layer. - Anna Forrest

1 Answers

0
votes

At least in the last version of Photoshop I scripted, smart objects were not part of the DOM - you have to use scriptlistener code to access the contents of those objects.

So the following snippet should start editing the smart object:

var id401 = stringIDToTypeID( "placedLayerEditContents" );
    var desc24 = new ActionDescriptor();
executeAction( id401, desc24, DialogModes.NO );

From there you need to loop through all the layers in the new active document which should contain the contents of your smart object, modify as needed and save & close it which would return the active document to your original document. Depending on how your smart objects are nested you may need to do this several times.