Skip to main content
Guild_-_Office_Pro
Inspiring
August 24, 2018
Answered

Cant split by line breaks

  • August 24, 2018
  • 3 replies
  • 3788 views

Hello,

I've looked everywhere for this, but I haven't had any results from suggestions in other discussions. I thought maybe it was somehow InDesign specific.

I'm trying to split a textframe's content by line breaks, in a file I didn't put together (and have no contact with the creator).

I initially tried

     split('\n');

with no luck. ( split('\t');     did work to split by tabs )

I have since tried everything I can think of / found online,

     split('\\n');

     split('\r\n');

     split('/\r\n|\n|\r/');

     split('<br>');

     split('</br>');

     split('');   ( getting desperate 😕😕 )

When I alert the contents it is split on two lines, so I know the content is broken up.

And when I enable 'show hidden characters' it does show a  ¶  character at the end of the lines I'm trying to split.

Does anyone know what character/code I should be using to split by?

Or is there somewhere in settings/preferences where I could specify the line break character for the document or paragraph style? ( I looked but couldn't find anything that sounded relevant)

This is slowly driving me crazy.

Any help would be very greatly appreciated!

This topic has been closed for replies.
Correct answer Guild_-_Office_Pro

I managed to get it to recognizes the paragraph return character!

In case it helps anyone else, here is the code that's working for me.

     split(/[\r\n]+/))

(with not surrounding quotation marks)

3 replies

Participant
March 26, 2021

Script works great! I know this is from a while back, but I'm wondering if there is a way to break up the line of text so it can be on two lines, as well as how to repeat the "pattern" so to speak so that it repeats top to bottom, left to right, to fill the image with the watermarked text. Does that make sense?

Disposition_Dev
Legend
March 27, 2021

can you share a screenshot of the "before" and a screenshot of the "after"? I'm not entirely sure what you're looking to do.

Peru Bob
Community Expert
Community Expert
August 24, 2018

Moved to InDesign Scripting.

Guild_-_Office_Pro
Guild_-_Office_ProAuthorCorrect answer
Inspiring
August 24, 2018

I managed to get it to recognizes the paragraph return character!

In case it helps anyone else, here is the code that's working for me.

     split(/[\r\n]+/))

(with not surrounding quotation marks)

Community Expert
August 24, 2018

Yes. string.split() also takes a regular expression as argument ( ExtendScript / JavaScript )

var s = "hi\nthere\n.";

s.split(/\n/);

// Result: [ "hi" , "there" , ". " ]

But I also get the same result with:

var s = "hi\nthere\n.";

s.split("\n");

Tested with ESTK 4.0.0.1.

This also works with InDesign's contents property of e.g. a text frame.

Even though the line break is a special character SpecialCharacters.FORCED_LINE_BREAK .

// Text frame selected

var s = app.selection[0].contents;

s.split("\n");

What is your string or contents you are running split() at?
If the contents of a text frame, can you show the text with hidden characters visible?

Best,
Uwe

Jongware
Community Expert
Community Expert
August 24, 2018

I don't see the regular paragraph return \r  (single and on its own) in OP's own list of attempts. My guess the final combination [\r\n] only works because it is in there in the character class.

InDesign indeed may translate a certain set of characters to its own (extended) set, but it's usually not a problem when grabbing a larger string. You can select any single character and then check its Unicode in the Info panel, which indeed shows U+000D for the hard return, as well as for a smattering of other column and page breaks. The JavaScript-specific "\r" is merely a shorthand for this Unicode, just as "\n" for a Shift-Return and "\b" for a backspace.

(And with that last one the analogy breaks. "\b" does NOT translate to "backspace" in InDesign's native text; it is one of the special types of tab.)