-
1. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 1:31 AM (in response to Apod42)Are the individual Type Layers uniform or do they combine text elements of any variation (size, font, spacing, …)?
-
2. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 1:45 AM (in response to Apod42)This should work on all open documents if the Type Layers’ content is uniform.
// replace text elements in type layers;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
for (var n = 0; n < app.documents.length; n++) {
app.activeDocument = app.documents[n];
app.activeDocument.suspendHistory("replace text", "main()")
}
};
// the opertation;
function main () {
var myDocument = app.activeDocument;
var theLayers = collectTextLayers(myDocument, []);
if (theLayers.length > 0) {
var theArray1 = ["LoremIpsum"];
var theArray2 = ["Dolor Sit Amet"];
for (var a = 0; a < theLayers.length; a++) {
for (var b = 0; b < theArray1.length; b++) {
var theString = theLayers[a].textItem.contents;
while (theString.indexOf(theArray1[b]) != -1) {
theString = theString.replace(theArray1[b], theArray2[b])
};
theLayers[a].textItem.contents = theString;
}
}
}
};
////// function collect all layers //////
function collectTextLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array};
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
if (theLayer.kind == LayerKind.TEXT) {allLayers.push(theLayer)};
}
else {
allLayers = (collectTextLayers(theLayer, allLayers))
// this line includes the layer groups;
// allLayers.push(theLayer);
}
};
return allLayers
};
-
3. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 1:56 AM (in response to c.pfaffenbichler)Thank you for the script! Will try to run it ASAP. The Type Layers are not uniform, font size, color, etc varies... Will the script work in this case?
-
4. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 1:57 AM (in response to Apod42)No, it won’t or rather is will destroy those variations.
-
5. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 1:58 AM (in response to c.pfaffenbichler)Will let you know what happens... Thank you!!
-
6. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 2:11 AM (in response to Apod42)You were right, of course! Your script works perfectly, but it ruins the formatting of documents. Nonetheless, thank you! Maybe I will get to a solution from here.
-
7. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 2:14 AM (in response to Apod42)Document Object Model Scripting can not take care of that formatting fur parts of a Type Layer’s contents.
Action Manager code can, but it is not that easy.
Do you know how many factors do vary?
-
8. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 2:19 AM (in response to c.pfaffenbichler)There are 5 font variations within the documents. If it helps, I can list the font type, size, color, etc for each variation.
-
9. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 2:25 AM (in response to Apod42)What are the other two variable properties (apart from font, size and color)?
-
10. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 2:37 AM (in response to c.pfaffenbichler)When it comes to the characters I have these variations:
Font Family
Font Style (Regular, Italic, Bold, etc)
Font Size
Font Color
* Font tracking (?) - when font is justified, Photoshop assigns a value for this (I belive it is the spacing between characters).
These variations are for the characters. There are also a lot of variations when it comes to paragraphs.
I do start to believe that this is too much of a handful.
-
11. Re: Find and replace text in multiple Photoshop files?
Michael L Hale Dec 10, 2013 4:21 AM (in response to Apod42)Apod42 wrote:
I do start to believe that this is too much of a handful.
It will be a handful. And how it's done will depend on which version of Photoshop you are using. It will require using Action Manager and adjusting not only the text string but either the text ranges( CS5 of lower ) or the text and paragraph ranges( CS6 or higher ).
The text descriptor is massive and even as experienced as I am with scripting I would do this manually.
-
12. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 5:26 AM (in response to Michael L Hale)c.pfaffenbichler
I think your script might still be the solution. I will just make some changes to the text layers in my photoshop files (break them into multiple text layers) and everything should work fine.
Please, how would the script look if beside replacing "LoremIpsum" with "Dolor Sit Amet" I would also like to replace in the same go the words "Sun" with "Moon" and "Cold" with "Hot" ?
Thank you!
-
13. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 10, 2013 5:28 AM (in response to Apod42)var theArray1 = ["LoremIpsum", "Sun", "Cold"];
var theArray2 = ["Dolor Sit Amet", "Moon", "Hot"];
-
14. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 10, 2013 5:29 AM (in response to c.pfaffenbichler)Thank you! Wish you all the best!
-
15. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 11, 2013 6:01 AM (in response to Apod42)Me again... If I used the script above (or a variant of it) to batch search/replace in just ONE Photoshop document, not multiple documents, would it still ruin font formatting?
Is there any other script for batch search/replace inside one document, using Photoshop's built in tool?
And is there a way to get the words in
var theArray1 = ["LoremIpsum", "Sun", "Cold"];
var theArray2 = ["Dolor Sit Amet", "Moon", "Hot"];
from an external .txt file, encoded in UTF-8? JavaScript just can't handle diacritics....
Thank you!
-
16. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 11, 2013 6:22 AM (in response to Apod42)Seems »Find and Replace Text« is actually Scrip-able.
// replace text elements in type layers;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
for (var n = 0; n < app.documents.length; n++) {
app.activeDocument = app.documents[n];
app.activeDocument.suspendHistory("replace text", "main()")
}
};
// the opertation;
function main () {
var myDocument = app.activeDocument;
var theArray1 = ["TEST", "test"];
var theArray2 = ["HAHAHA", "LALALA"];
for (var b = 0; b < theArray1.length; b++) {
replaceText (theArray1[b], theArray2[b])
}
};
////// reoplace text //////
function replaceText (replaceThis, replaceWith) {
// =======================================================
var idreplace = stringIDToTypeID( "replace" );
var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idreplace = stringIDToTypeID( "replace" );
ref3.putProperty( idPrpr, idreplace );
var idTxLr = charIDToTypeID( "TxLr" );
var idOrdn = charIDToTypeID( "Ordn" );
var idAl = charIDToTypeID( "Al " );
ref3.putEnumerated( idTxLr, idOrdn, idAl );
desc22.putReference( idnull, ref3 );
var idUsng = charIDToTypeID( "Usng" );
var desc23 = new ActionDescriptor();
var idfind = stringIDToTypeID( "find" );
desc23.putString( idfind, replaceThis );
var idreplace = stringIDToTypeID( "replace" );
desc23.putString( idreplace, replaceWith );
var idcheckAll = stringIDToTypeID( "checkAll" );
desc23.putBoolean( idcheckAll, true );
var idFwd = charIDToTypeID( "Fwd " );
desc23.putBoolean( idFwd, true );
var idcaseSensitive = stringIDToTypeID( "caseSensitive" );
desc23.putBoolean( idcaseSensitive, false );
var idwholeWord = stringIDToTypeID( "wholeWord" );
desc23.putBoolean( idwholeWord, false );
var idignoreAccents = stringIDToTypeID( "ignoreAccents" );
desc23.putBoolean( idignoreAccents, true );
var idfindReplace = stringIDToTypeID( "findReplace" );
desc22.putObject( idUsng, idfindReplace, desc23 );
executeAction( idreplace, desc22, DialogModes.NO );
};
-
17. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 11, 2013 7:01 AM (in response to Apod42)To answer some questions more directly:
Me again... If I used the script above (or a variant of it) to batch search/replace in just ONE Photoshop document, not multiple documents, would it still ruin font formatting?
The handling of all open documents has nothing to do with the difficulties of Scripting Type Layers.
Is there any other script for batch search/replace inside one document, using Photoshop's built in tool?
Above Script uses the feature and it seems to not destroy varying formatting, if you want to run it on only the active document delete the lines
for (var n = 0; n < app.documents.length; n++) {
app.activeDocument = app.documents[n];
and the corresponding closing bracket.
-
18. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 11, 2013 1:17 PM (in response to c.pfaffenbichler)I think that this conversation (and of course your know-how), has produced an amazing result. I belive that many people can make use of this script, I see that already Google indexed this page I have tested it extensively, and it works flawless, it's fast, and if I encode the .jsx file containing the script in UTF-8 it handles special characters (diacritics in my case) just fne.
There is still one unanswered question (of minimal importance):
Is there a way to get the words in
var theArray1 = ["TEST", "test"];
var theArray2 = ["HAHAHA", "LALALA"];
from an external .txt file? So there is no need to modify the script for each operation, just modify the external file.
Thank you again for all your time!
-
19. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 11, 2013 11:51 PM (in response to Apod42)You’re welcome, advice given here is free.
If you want to donate something nonetheless you could do so over at
Many of the same people used to contribute there as here and I for one have benefitted considerably from their generous advice on Scripting issues.
A Script can read (or create) txt files, but I do not have a lot of experience with this.
This might work (amend the line »var theTexts = readPref ("….txt", false);« according to your txt-file’s path):
// replace text elements in type layers;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
for (var n = 0; n < app.documents.length; n++) {
app.activeDocument = app.documents[n];
app.activeDocument.suspendHistory("replace text", "main()")
}
};
// the opertation;
function main () {
var myDocument = app.activeDocument;
var theTexts = readPref ("….txt", false);
var theArray1 = theTexts.slice(0, Math.round(theTexts.length/2));
var theArray2 = theTexts.slice(Math.round(theTexts.length/2), theTexts.length);
alert (theArray1.join("\n")+"\n\n\n"+theArray2.join("\n"))
for (var b = 0; b < theArray1.length; b++) {
replaceText (theArray1[b], theArray2[b])
};
};
////// reoplace text //////
function replaceText (replaceThis, replaceWith) {
// =======================================================
var idreplace = stringIDToTypeID( "replace" );
var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idreplace = stringIDToTypeID( "replace" );
ref3.putProperty( idPrpr, idreplace );
var idTxLr = charIDToTypeID( "TxLr" );
var idOrdn = charIDToTypeID( "Ordn" );
var idAl = charIDToTypeID( "Al " );
ref3.putEnumerated( idTxLr, idOrdn, idAl );
desc22.putReference( idnull, ref3 );
var idUsng = charIDToTypeID( "Usng" );
var desc23 = new ActionDescriptor();
var idfind = stringIDToTypeID( "find" );
desc23.putString( idfind, replaceThis );
var idreplace = stringIDToTypeID( "replace" );
desc23.putString( idreplace, replaceWith );
var idcheckAll = stringIDToTypeID( "checkAll" );
desc23.putBoolean( idcheckAll, true );
var idFwd = charIDToTypeID( "Fwd " );
desc23.putBoolean( idFwd, true );
var idcaseSensitive = stringIDToTypeID( "caseSensitive" );
desc23.putBoolean( idcaseSensitive, false );
var idwholeWord = stringIDToTypeID( "wholeWord" );
desc23.putBoolean( idwholeWord, false );
var idignoreAccents = stringIDToTypeID( "ignoreAccents" );
desc23.putBoolean( idignoreAccents, true );
var idfindReplace = stringIDToTypeID( "findReplace" );
desc22.putObject( idUsng, idfindReplace, desc23 );
executeAction( idreplace, desc22, DialogModes.NO );
};
////// read prefs file //////
function readPref (thePath, binary) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
if (binary == true) {file.encoding= 'BINARY'};
var theText = file.read();
file.close();
return String(theText).split(",")
}
};
In this case the comma is used to split the text into Strings in Arrays, if your search/replace texts include commas you could use something else, I guess.
-
20. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 12:55 AM (in response to c.pfaffenbichler)Thank you! I will test with this script today, will also make a donation to the forum you indicated!
-
21. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 1:22 AM (in response to Apod42)This is the body of the external file:
Sun,Moon
It replaces Sun with Moon. I just don't get how should I format the external text file to add more words to replace. Add for example Cold,Hot and Day,Night...
-
22. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 12, 2013 1:25 AM (in response to Apod42)Day,Cold,Night,Hot
-
23. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 1:51 AM (in response to c.pfaffenbichler)I get it now, with a blank line (space) makes logical order... Will never think as a programmer
-
24. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 12, 2013 4:42 AM (in response to Apod42)Actually my approach may be a bit clumsy (not a programmer, either), listing the pairs might be more elegant; the for-clause
for (var b = 0; b < theArray1.length; b++) {
would have to be changed a bit then, though.
-
25. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 9:43 AM (in response to c.pfaffenbichler)More questions, only if you find the time...
By default, Photoshop's search tool searches only visible text layers. If a layer is hidden, no search is performed on that layer. Your script above acts in the same way. Is there a way to make the script bypass Photoshop's default, and also search (and replace) in hidden layers?
For anyone reading this in the future; there is a way to search hidden layers. Go to Window > Layer Comps. Select your hidden text layers, make them visible, and create a new layer comp (think of it as a document within a document). Do a layer comp for your main document also. When you search, select the comp with all the text layers visible. Then you can switch with just a click to your main version of the document. Sounds complicated, it's straightforward. Just remember about "Layer Comps".
Question two. The alert in your last script pops up like this:
You are about to search and replace: (doesn't actually say this, this line is my great addition to your script )
Sun
Day
Hot
Moon
Night
Cold
Is there a way to make the alert like this?
You are about to search and replace:
Sun
Moon
Day
Night
Hot
Cold
I guess this is this snippet of code here:
alert (theArray1.join("\n")+"\n\n\n"+theArray2.join("\n"))
All the best!
-
26. Re: Find and replace text in multiple Photoshop files?
Michael L Hale Dec 12, 2013 11:43 AM (in response to c.pfaffenbichler)I would have made the text file a two line file. The first line would be for the search terms and the second for the replacement terms.
'Sun','Day','Hot' 'Moon','Night','Cold'
Then I would replace these lines
var theTexts = readPref ("….txt", false); var theArray1 = theTexts.slice(0, Math.round(theTexts.length/2)); var theArray2 = theTexts.slice(Math.round(theTexts.length/2), theTexts.length);with these
var theTexts = readPref ("...filePath or file object"); var theArray1 = theTexts[0]; var theArray2 = theTexts.[1];and replaced the readPerf function with this
////// read prefs file ////// function readPref (thePath) { if (File(thePath).exists == true) { var file = File(thePath); file.open("r"); var textArray = []; textArray.push(file.readln().split(","));// make array of search terms textArray.push(file.readln().split(","));// make array of replace terms file.close(); return textArray; } };Ideally it would have checks to make sure there are two lines and the number of terms match.
-
27. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 2:30 PM (in response to Michael L Hale)Thank you! For me (Photoshop CS3), the updated script gives an error:
Error 1243: Illegal argument - Argument 2
-Required value is missing
Line: 41
-> desc23.putString(idreplace, replaceWith);
-
28. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 12, 2013 9:28 PM (in response to Apod42)My bad, it works.... Results are identical with previous version.
-
29. Re: Find and replace text in multiple Photoshop files?
Michael L Hale Dec 13, 2013 10:45 AM (in response to Apod42)Yes, I expected the results would be the same. I just thought that having a two line text file with one line for the search terms and the second for the replacement terms was better than a one line text file. I think the two line text file is better because you don't have to count the terms to determine which is a search term and which is a replacement term.
-
30. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Dec 13, 2013 10:47 AM (in response to Michael L Hale)You are right, Mike, your approach offers advantages indeed.
Setting up the file should be much more convenient thus.
-
31. Re: Find and replace text in multiple Photoshop files?
Apod42 Dec 13, 2013 1:16 PM (in response to c.pfaffenbichler)Thank you both very much, it has been a pleasure! I already started using the original script by c.pfaffenbichler, with additions by Michael L Hale
Hope this thread will be of help to other people, in the future.
Over and out
-
32. Re: Find and replace text in multiple Photoshop files?
Apod42 Feb 21, 2014 1:26 AM (in response to Apod42)Me again
The script does a great job at replacing a word with another word. But how should I approach replacing a word with a few paragraphs of text? It works, but when it encounters a space betwen paragraphs (a blank line, like the one below) it stops.
I have tried using \n (as I use in Javascript alerts) but it does not work... Is there another way?
Thank you!!
-
33. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Feb 21, 2014 2:35 AM (in response to Apod42)Could you post the Script and the txt-file you work with now?
-
34. Re: Find and replace text in multiple Photoshop files?
Apod42 Feb 21, 2014 3:12 AM (in response to c.pfaffenbichler)SCRIPT:
#target photoshop
if (app.documents.length > 0) {
for (var n = 0; n < app.documents.length; n++) {
app.activeDocument = app.documents[n];
app.activeDocument.suspendHistory("replace text", "main()")
}
};
function main () {
var myDocument = app.activeDocument;
var theTexts = readPref ("D:/Directory/text-file.txt");
var theArray1 = theTexts[0];
var theArray2 = theTexts[1];
for (var b = 0; b < theArray1.length; b++) {
replaceText (theArray1[b], theArray2[b])
};
};
function replaceText (replaceThis, replaceWith) {
var idreplace = stringIDToTypeID( "replace" );
var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idreplace = stringIDToTypeID( "replace" );
ref3.putProperty( idPrpr, idreplace );
var idTxLr = charIDToTypeID( "TxLr" );
var idOrdn = charIDToTypeID( "Ordn" );
var idAl = charIDToTypeID( "Al " );
ref3.putEnumerated( idTxLr, idOrdn, idAl );
desc22.putReference( idnull, ref3 );
var idUsng = charIDToTypeID( "Usng" );
var desc23 = new ActionDescriptor();
var idfind = stringIDToTypeID( "find" );
desc23.putString( idfind, replaceThis );
var idreplace = stringIDToTypeID( "replace" );
desc23.putString( idreplace, replaceWith );
var idcheckAll = stringIDToTypeID( "checkAll" );
desc23.putBoolean( idcheckAll, true );
var idFwd = charIDToTypeID( "Fwd " );
desc23.putBoolean( idFwd, false );
var idcaseSensitive = stringIDToTypeID( "caseSensitive" );
desc23.putBoolean( idcaseSensitive, true );
var idwholeWord = stringIDToTypeID( "wholeWord" );
desc23.putBoolean( idwholeWord, false );
var idignoreAccents = stringIDToTypeID( "ignoreAccents" );
desc23.putBoolean( idignoreAccents, false );
var idfindReplace = stringIDToTypeID( "findReplace" );
desc22.putObject( idUsng, idfindReplace, desc23 );
executeAction( idreplace, desc22, DialogModes.NO );
};
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
var textArray = [];
textArray.push(file.readln().split("|"));
textArray.push(file.readln().split("|"));
file.close();
return textArray;
}
};
TEXT FILE:
Sun|Night|Cold
Moon|Day|Hot
-
35. Re: Find and replace text in multiple Photoshop files?
jdubbb Feb 21, 2014 3:44 AM (in response to Apod42)First off, this would be super useful if I can get it working, so thanks so much in advance.
Just tried running the above script posted by Apod42... But it returned this error. Any ideas?
I'm running Photoshop CC.
----
- The object “in contents of all text layer” is not currently available.
Line: 48
-> executeAction( idreplace, desc22, DialogModes.NO );
----
Many thanks!
-
36. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Feb 21, 2014 3:46 AM (in response to Apod42)One could always foresake the readln in readPref and use some other letter/s to split the string in the txt-file into two. One would have to restructure the txt-file accordingly.
-
37. Re: Find and replace text in multiple Photoshop files?
Apod42 Feb 21, 2014 4:58 AM (in response to c.pfaffenbichler)Hello,
Your last replay was for me or jdubbb? Show I work on this part:
textArray.push(file.readln().split("|"));
textArray.push(file.readln().split("|"));
Get rid of the readln?
As before, thank you!
-
38. Re: Find and replace text in multiple Photoshop files?
c.pfaffenbichler Feb 21, 2014 7:18 AM (in response to Apod42)I’m off for the weekend, so unless someone else provides a solution in the meantime I’ll try to get back to you in a couple of days.
-
39. Re: Find and replace text in multiple Photoshop files?
jdubbb Feb 21, 2014 8:01 AM (in response to c.pfaffenbichler)Just got it working!
I made sure that I had just one of the target text layers selected in the layers panel.
Thank you so much. Saved me SO much time.


