• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Find/replace text in Photshop with "hard/carriage" return.

Community Beginner ,
Aug 19, 2020 Aug 19, 2020

Copy link to clipboard

Copied

Hi!

Does anyone have a script that can find/replace text layers in photoshop where there is a hard/carriage return?

 

Like this example >

 

Lorem ipsum

dolor sit amet

 

If I search for "Lorem ipsum dolor sit amet"  PS won't change anything, because of the "hard/carriage" return.

 

I did search for this, but could'nt find anything that worked.

TOPICS
Actions and scripting

Views

664

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Aug 19, 2020 Aug 19, 2020

Copy link to clipboard

Copied

I'll post the code shortly. 

As this will be using a regular expression, you'll need to use \r for the break character (lorem ipsum\rdolor).

 

 

This uses hard-coded text, time permitting, I'll try to post a version that uses a GUI later...

/* https://community.adobe.com/t5/photoshop/javascript-find-and-replace-text-in-photoshop/td-p/10847173 */
var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
    try {
        doc.activeLayer = doc.layers[i];
        // Find = lorem ipsum dolor 
        // Replace = placeholder text
        // g = global, i = case insensitive
        if (app.activeDocument.activeLayer.kind == LayerKind.TEXT) {
            doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/lorem ipsum\rdolor|lorem ipsum dolor/gi, 'placeholder text');
        }
    } catch (e) {}
}

 

I just realised while it is easy to search for a line break, it is not as easy to replace with one too!

 

In my example:

 

lorem ipsum

dolor

 

would become a single line:

 

lorem ipsum dolor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

Hi Stephen,

Thanks! But you're right, I don't want it to be a single line. It need to keep the same "formatting".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

You can use split() and join().

If you want a tab-delimitted text file export of all text layers in a file, this script will do so:

-----------------------------------------

 

#target photoshop
textCopier();
function textCopier(){
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
try{
var fleLogFile = new File('~/Desktop/textExport.txt');
if(!File('~/Desktop/textExport.txt').exists){
var fleLogFile = new File('~/Desktop/textExport.txt').saveDlg('Create New Log File', '*.txt');
if(fleLogFile != null){
fleLogFile.open('w:');
}
}
else{
fleLogFile.open('a:');
}
for(var j = 0; j < documents.length; j++){
var docRef = documents[j];
var exportLine = docRef.name;
var sets = docRef.layerSets;
for(var l = 0; l < sets.length; l++){
for(var k = 0; k < sets[l].artLayers.length; k++){
var SetLayerRef = sets[l].artLayers[k];
if(SetLayerRef.kind == LayerKind.TEXT){
var SetlayerText = SetLayerRef.textItem.contents;
SetlayerText = SetlayerText.replace('\r', ' [return] \t');
SetlayerText = SetlayerText.replace('(', '');
SetlayerText = SetlayerText.replace(')', '');
exportLine = exportLine + '\t' + SetlayerText;
}
}
}
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
if(LayerRef.kind == LayerKind.TEXT){
var layerText = LayerRef.textItem.contents;
layerText = layerText.replace('\r', ' [return] \t');
layerText = layerText.replace('(', '');
layerText = layerText.replace(')', '');
exportLine = exportLine + '\t' + layerText;
}
}
fileWriter();
docRef.close();
}
function fileWriter(){
fleLogFile.writeln(exportLine);
exportLine = '';
}
fleLogFile.close();
}
catch(e){
alert(e + e.line);
}
app.displayDialogs = originalDialogMode;
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2021 Nov 08, 2021

Copy link to clipboard

Copied

Hello. I like your script, but it skips files if you open several in the program. For example, if you open 1.psd 2.psd, 3.psd, then it will save the text only from 1.psd and 3.psd. 2.psd will skipped. Can you fix it?

 

 

I forgot to write about the versions:

PS 22.1.1, Windows 10 1909

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2021 Nov 08, 2021

Copy link to clipboard

Copied

After "docRef.close();" try adding a line "j = j - 1;"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2021 Nov 08, 2021

Copy link to clipboard

Copied

Thanks, it works. Just deleting "docRef.close();" also helps.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2021 Nov 08, 2021

Copy link to clipboard

Copied

LATEST

Yeah if you want to leave your files open. I'm counting open files but then wasn't adjusting for how many were left when I closed one.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines