0

Hi I am looking for a way to separate a text layer into multiple text layers per line. I have a (long) list that i can paste into photoshop, but now i'm looking to split that list up into separate text layers.

I did find a script here that splits them up per word (and works), but i have no idea how to edit it to split per line.

// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop

// in case we double clicked the file
app.bringToFront();

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
// $.level = 0;
// debugger; // launch debugger on next line

var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.POINTS;

var thisDocument = app.activeDocument;

// USE THIS LINE TO GRAB TEXT FROM EXISTING LAYER
var theOriginalTextLayer = thisDocument.activeLayer;
var theTextToSplit = theOriginalTextLayer.textItem.contents;

// OR USE THIS LINE TO DEFINE YOUR OWN
// var theTextToSplit = "Hello";

// suppress all dialogs
app.displayDialogs = DialogModes.NO;

//  the color of the text as a numerical rgb value
var textColor = new SolidColor;
textColor.rgb.red = 0;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;

var fontSize = 120;         // font size in points
var textBaseline = 480;     // the vertical distance in pixels between the top-left corner of the document and the bottom-left corner of the text-box


var words = theTextToSplit.split(" ");

for(a=0; a < words.length; a++){ 
// this loop will go through each character

    var newTextLayer = thisDocument.artLayers.add();    // create new photoshop layer
        newTextLayer.kind = LayerKind.TEXT;             // set the layer kind to be text

    var theTextBox = newTextLayer.textItem;             // edit the text
        theTextBox.font = "Arial";                      // set font
        theTextBox.contents = words[a];                 // Put each character in the text
        theTextBox.size = fontSize;                     // set font size
    var textPosition = a*(fontSize*0.7);

        theTextBox.position = Array(textPosition, textBaseline);    // apply the bottom-left corner position for each character
        theTextBox.color = textColor;

};

/* Reset */

app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
docRef = null;
textColor = null;
newTextLayer = null;
4
  • That script merely splits the text based upon spaces - it knows that each space indicates a new word. Is there a common glyph you can split your lines by, like a period or a line feed? Or can you add a common glyph to where the lines should split (often easily done in a text editor before copying to Photoshop)?
    – Scott
    Apr 8 '21 at 18:51
  • @scott I can do that, but what do i change to do that?
    – perbrethil
    Apr 8 '21 at 19:13
  • @SergeyKritskiy i saw that one, but it does not work for me
    – perbrethil
    Apr 8 '21 at 19:14
1

The script isn't counting word lengths or anything like that. It's merely looking for a character... and then using that character as the catalyst for a new layer. In the case of the original script, it looks for a space. You merely need to replace that space with something else.

If you just want to split the list
based upon existing line feeds within the text...

Simply edit the script. Change this line:

var words = theTextToSplit.split(" ");

to this:

var words = theTextToSplit.split("\r");
(Note the slash direction - it's backward. A standard URL forward slash won't work.)

Then when you call the script each time there is a line feed, in the highlighted text layer within Photoshop, the text layer will be split to a new layer.


If you want to use a
special character to split the text....

Add any odd character to the end of each list item... such as # or @ or $ or % as long as it doesn't actually appear anywhere within the text itself. It doesn't matter if the list has line feed or is just one big block of text, as long as the special character appears wherever you want the text to split. This can often be a quick, 2 second adjustment to the text using a text editor. Then copy/paste to Photoshop as a text layer.

Edit the script and change this line...

var words = theTextToSplit.split(" ");

Insert whatever special character you used at the end of the lines between the quotes.. a la...

var words = theTextToSplit.split("#"); or var words = theTextToSplit.split("@");

Then the script will split on that special character creating a new layer each time the special character is detected.


I tested both these edits (second one by using a # at the end of every list item) and it seems to work perfectly in both instances.
(Although I did also reduce the font size from 120 - no clue why it had to be so large.)

7
  • Does Photoshop give you access to an end of line marker, (i.e, carriage return/end line)?
    – nocturns2
    Apr 8 '21 at 20:19
  • @nocturns2 that was my original thought \n but it doesn't work here. Might just be a syntax thing. I didn't dig much further.
    – Scott
    Apr 8 '21 at 20:24
  • I figured they must have some moniker to handle end of lines in a text block.
    – nocturns2
    Apr 8 '21 at 20:26
  • @nocturns2 Yup, syntax.. I worked it out \r is all that is necessary.. updated answer.
    – Scott
    Apr 8 '21 at 20:36
  • oh my god, this absolutely works! thank you!
    – perbrethil
    Apr 9 '21 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.