0

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++) {).

Community
  • 1
  • 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 Feb 23 '15 at 01:57
  • 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 Feb 23 '15 at 06:03
  • 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 Feb 23 '15 at 13:04

1 Answers1

0

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.

Anna Forrest
  • 1,711
  • 14
  • 21
  • Thanks Anna, I am very new to scripting so will i have to to combine the previous script and the one you just mentioned or I can use the above by itself. Thanks again. – Sam Rizzi Feb 23 '15 at 15:14
  • You need to combine the two and do some other work to glue them together. The snippet in this answer will only get you access to the smart object contents - you'll need to use the script in the answer you found earlier to actually change the contents. If you have a go at putting the pieces together and post your code - I'll help you work through it. – Anna Forrest Feb 23 '15 at 16:20
  • Hey actually I ended up posting this over elance. Once I have the script will share. – Sam Rizzi Feb 24 '15 at 05:44