Skip to main content
Known Participant
June 7, 2022
Answered

Issue while using force line break

  • June 7, 2022
  • 4 replies
  • 3944 views

Hi, I have an issue while using force line break in illustrator the forced line break content is created as a new paragraph instead of a line break I have used \n in my script to break the line

This topic has been closed for replies.
Correct answer m1b

Hey Mark, Can you help with the script for Paragraph objects and also allow us to take only 3 paragraphs in a text frame instead of 5 paragraphs.


Hi @SA8888, have a look at this script. I've made a function that tries to solve this problem.

Let me know if it works for you.

- Mark

/*
    Get Paragraphs
    for Adobe Illustrator

    by m1b, here: https://community.adobe.com/t5/illustrator-discussions/issue-while-using-force-line-break/m-p/12996177

    Illustrator treats linefeeds as carriage returns,
    so when you ask a TextRange for paragraphs, it will
    give you too many. This function trys to solve this. 
*/

// example usage:
var paras = getParagraphs(app.activeDocument.selection[0]);
$.writeln(paras[0].contents);
$.writeln(paras[0].lines[1].contents);
$.writeln(paras[0].constructor.name);
$.writeln(paras[1].words[3].contents);


function getParagraphs(obj) {
    if (!obj.hasOwnProperty('story'))
        return
    var story = obj.story,
        text = story.textRange.contents,
        findCarriageReturns = /\u000D/g,
        lastStart = 0,
        paras = [],
        para;

    // add paras between carriage returns
    findCarriageReturns.lastIndex = 0;
    while ((findCarriageReturns.exec(text)) !== null) {
        para = story.textRanges[0];
        para.start = lastStart;
        para.end = findCarriageReturns.lastIndex - 1;
        paras.push(para);
        lastStart = findCarriageReturns.lastIndex;
    }
    // add last paragraph
    if (
        findCarriageReturns.lastIndex > 0
        && findCarriageReturns.lastIndex <= text.length - 1
    ) {
        para = story.textRanges[0];
        para.start = findCarriageReturns.lastIndex;
        para.end = text.length - 1;
        paras.push(para);
    }
    return paras;
}

 

4 replies

Brainiac
April 20, 2023

Interesting observation. Separate paragraphs per force line break should also have implications on the range of applied paragraph styles or paragraph level attributes, and exchange of text with InDesign via CC Library.

m1b
Brainiac
June 7, 2022

Try using the Unicode in your string. If you're using extendscript, "\u000A" for linefeed.

- Mark

SA8888Author
Known Participant
June 8, 2022

Thank you for the Unicode suggestion, Mark.  I have used "\u0003" instead of "\u000A". There is another issue with illustrator when I use the Unicode in my script the length of the paragraph shows two, it should be one, not two.

The script is:

                    var doc = app.activeDocument;

                    var object = doc.textFrames.add();

                    object.contents = 'Line \u0003 break'

                    alert("length::"+object.paragraphs.length)

                    

                      The alert message shows :

                      length::2

    

The correct answer is One if I use a force line break.

 

Is there any other script so I can get the paragraph length as One.

        

SA8888Author
Known Participant
June 10, 2022

please let me see the whole script you're using


var str = selection[0].contents;
var flb = 0;

var match = str.match(/\u0003/g); // get forced line break characters

if (match) {
    flb = match.length;
}

alert ("Selection Paragraphs: " + (selection[0].paragraphs.length - flb));

var para = selection[0]. paragraphs.length - flb

 

for(i=0; i<para; i++)

{}

 

 

 

SA8888Author
Known Participant
June 7, 2022

I need this symbol to represent in code  instead of