Highlighted

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

Community Beginner ,
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, Mac

Views

54

Likes

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

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

Community Beginner ,
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, Mac

Views

55

Likes

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
Adobe Community Professional ,
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).

Likes

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
Reply
Loading...
Adobe Community Professional ,
Aug 19, 2020

Copy link to clipboard

Copied

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) {}
}

Likes

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
Reply
Loading...
Adobe Community Professional ,
Aug 19, 2020

Copy link to clipboard

Copied

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

 

 

Likes

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
Reply
Loading...
Community Beginner ,
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".

Likes

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
Reply
Loading...
Adobe Community Professional ,
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;
}
}

Likes

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
Reply
Loading...