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

Cant split by line breaks

Community Beginner ,
Aug 24, 2018 Aug 24, 2018

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

2.6K

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

correct answers 1 Correct answer

Community Beginner , Aug 24, 2018 Aug 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)

Votes

Translate

Translate
Community Beginner ,
Aug 24, 2018 Aug 24, 2018

Copy link to clipboard

Copied

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)

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 Expert ,
Aug 24, 2018 Aug 24, 2018

Copy link to clipboard

Copied

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

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 Expert ,
Aug 24, 2018 Aug 24, 2018

Copy link to clipboard

Copied

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.)

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 ,
Nov 12, 2020 Nov 12, 2020

Copy link to clipboard

Copied

Thank you so much for sharing. I was going crazy trying to figure why my expression seemed blind to the line breaks.

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 Expert ,
Aug 24, 2018 Aug 24, 2018

Copy link to clipboard

Copied

Moved to InDesign Scripting.

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 ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

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?

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 Expert ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

LATEST

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.

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