Skip to main content
Known Participant
July 22, 2024
Answered

Batch adding line break behind every comma of text layer

  • July 22, 2024
  • 2 replies
  • 1189 views

I've complete some psd files which contained a lot of text layers, but I forgot to separate them into lines ( they must be broken at each comma & dot).

For example:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit"

The result must be

"Lorem ipsum dolor sit amet,

consectetur adipiscing elit"

Tried with Find and replace tool but I don't know which line break symbol is.

Does anyone have any idea to help me?

This topic has been closed for replies.
Correct answer Stephen Marsh

If only Photoshop was InDesign when it came to text!

 

I don't know if this is possible using the standard Edit > Find and Replace Text... command.

 

It is possible to script using a regular expression based find/replace, however, if you have different text attributes (sizes, colours etc) then the following code may not work correctly (this script changes all text layers from comma+space to comma+return):

 

/*
Text Replace Comma Space for Comma Return in All Text Layers and Groups.jsx
v1.0 - 25th July 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/batch-adding-line-break-behind-every-comma-of-text-layer/m-p/14753971
*/

#target photoshop

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    processLayersAndGroups(doc.layers);
} else {
    alert("A document must be open to run this script!");
}

function changeTextContents(layer) {
    // Check if the layer is a text layer
    if (layer.kind == LayerKind.TEXT) {
        // Regular expression to change comma+space to comma+return
        layer.textItem.contents = layer.textItem.contents.replace(/\, /g, ',\r');
    }
}

function processLayersAndGroups(layers) {
    // Recursively process the root/top-level layers
    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        if (layer.typename == "ArtLayer") {
            changeTextContents(layer);
            // Recursively process the layers within all groups
        } else if (layer.typename == "LayerSet") {
            processLayersAndGroups(layer.layers);
        }
    }
}

 

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 22, 2024

If only Photoshop was InDesign when it came to text!

 

I don't know if this is possible using the standard Edit > Find and Replace Text... command.

 

It is possible to script using a regular expression based find/replace, however, if you have different text attributes (sizes, colours etc) then the following code may not work correctly (this script changes all text layers from comma+space to comma+return):

 

/*
Text Replace Comma Space for Comma Return in All Text Layers and Groups.jsx
v1.0 - 25th July 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/batch-adding-line-break-behind-every-comma-of-text-layer/m-p/14753971
*/

#target photoshop

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    processLayersAndGroups(doc.layers);
} else {
    alert("A document must be open to run this script!");
}

function changeTextContents(layer) {
    // Check if the layer is a text layer
    if (layer.kind == LayerKind.TEXT) {
        // Regular expression to change comma+space to comma+return
        layer.textItem.contents = layer.textItem.contents.replace(/\, /g, ',\r');
    }
}

function processLayersAndGroups(layers) {
    // Recursively process the root/top-level layers
    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        if (layer.typename == "ArtLayer") {
            changeTextContents(layer);
            // Recursively process the layers within all groups
        } else if (layer.typename == "LayerSet") {
            processLayersAndGroups(layer.layers);
        }
    }
}

 

 

Known Participant
July 23, 2024

I am very grateful for your help ^^ However, the script not worked to me. Yeah, working with Photoshop is a bit annoyed when they limit many things, I wished I could have more option than it

Known Participant
July 23, 2024
quote

oops, I've left a reply to you but it has disapeared when I submitted XD I'm terribly sorry about my forgetfulness that not telling you the text layers are inside a group! That's why the script not worked, not its fault!


By @Zipser31550168t845



Yes, that sort of information is critical for scripting.

 

For testing with the current code, can you ungroup in a copy of the original file? If the script works for you I can add code to process text layers inside groups.

 

The suggestion from @c.pfaffenbichler works within groups, all you need to do is copy the comma and return from within a text layer and then paste it into the replace field.


thanks for your suggestion, I tested without group and it worked perfectly. I'll mark your first reply as correct answer

c.pfaffenbichler
Community Expert
Community Expert
July 22, 2024

Please provide a file.

 

You can copy a linebreak and paste it in the »Find and Replace Text« field. 

Stephen Marsh
Community Expert
Community Expert
July 22, 2024
quote

You can copy a linebreak and paste it in the »Find and Replace Text« field. 

 

By @c.pfaffenbichler

 

I didn't know that!

 

EDIT: I just tried and this works beautifully! I copied the return character from within the text inside Photoshop.