Issue while using force line break

Community Beginner ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

445

Likes

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

Adobe Community Professional , Jun 12, 2022 Jun 12, 2022
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 ...

Likes

Translate

Translate
Community Beginner ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

I need this symbol to represent in codelb.png  instead of p.png

Likes

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
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

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

- Mark

Likes

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

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.

        

Likes

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
Adobe Community Professional ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

inserting a forced line break manually also counts as two paragraphs, how do you know it should be one?

forcedLineBreak.jpg

Likes

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

In Indesign, when we apply forced line break manually the paragraph count gives one.

Likes

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

If inserting a forced line break manually also counts as two paragraphs, then what is the use of using a forced line break. 

Likes

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
Adobe Community Professional ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

oh I see, InDesign is different than Illustrator.

 

I don't know what the use is, but as as workaround you could probably count the force line breaks and deduct them from the paragraph count if that is important for your workflow.

Likes

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thank you, Carlos Can you brief this in detail may be through a script if you can

Likes

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
Adobe Community Professional ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

sure

 

// count Forced Line Breaks
// https://community.adobe.com/t5/illustrator-discussions/issue-while-using-force-line-break/m-p/12992472#M324491

// select a text frame before running
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));

Likes

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thank you for the script, Carlos 

Likes

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
Adobe Community Professional ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Wow, I just double-checked on my machine and you are exactly right. I didn't realise (until now!) that Illustrator uses \u0003 (End of text) character for linefeeds instead of an actual linefeed \u000A. Or that paragraph count in Illustrator is messed up by it's own linefeeds. Weird.

Likes

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 ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Thank you for the script on Force Line Break.

I've got another issue in my code. I have used the script given by you. I'm getting the paragraph length correctly.  While I'm trying to save my code, I cannot assign the value of (selection [0].paragraphs.length-flb); to a particular variable if I set the value, the output of my script changes.

Ex:

object.contents = 'Line \u0003 break' The paragraph length will be one, and the output I will get is only Line. Please guide me through the script in this regard.

Likes

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
Adobe Community Professional ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

please let me see the whole script you're using

Likes

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 ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

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

{}

 

 

 

Likes

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
Adobe Community Professional ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

I tested your code and it works as expected. What result do you get? - Mark

Screen Shot 2022-06-10 at 3.49.16 pm.png

Likes

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

The issue I'm facing is while saving the script

The script I've used is 

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

{}

If I use the above script the output will be

1st paragraph with line break

still technically first paragraph

Okay this is second paragraph

 

And the last two lines will not be displayed

Likes

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
Adobe Community Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

Yes they won't be displayed because selection[0].paragraphs.length - flb == 3, so you are only looping over the "visible" paragraphs, which exist only in our minds. As far as Illustrator is concerned there are 5 paragraphs in that sample text, so you need to loop over all 5, not the first 3.

 

Also your loop condition is i < para which will stop at i==1 when para == 2, giving you only the first 2 paragraphs (index 0 and index 1). Try using i <= para.

 

- Mark

Likes

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
Adobe Community Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

So I am thinking that this might be the sort of thing you need:

// get the whole text contents
var str = selection[0].contents;
// replace EOT chars with single space, including surrounding spaces
// then split at CR chars
var paraContents = str.replace(/\s*\u0003\s*/g, ' ').split('\u000D');
// now see what you've got
for (i = 0; i < paraContents.length; i++) {
    alert('paragraph 1 text:\n' + paraContents[i]);
}

It doesn't use Paragraph objects, just the contents string. If you need actual Paragraph objects, you'll need a different approach.

- Mark

Likes

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

I need the paragraph objects, and while saving the script I should have only 3 paragraphs not 5 paragraphs. I'm getting 5 paragraphs while saving the script. 

Likes

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
Adobe Community Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

What you are asking for doesn't sound possible on the face of it because, by Illustrator's definition, a paragraph with a linefeed is two Paragraph objects. By definition. Yes it's weird, but that's how it is. There is probably a solution, but we'd need to know what you intend to do with the Paragraph objects to propose a way forward. How are you going to use the paragraphs? For example, which properties of Paragraph object will you need?

- Mark

Likes

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

I'm trying to get the Paragraph objects as a TextRange instead of a string, is it possible  to convert it as TextRange using script.

Likes

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
Adobe Community Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

Okay a TextRange instead of Paragraphs, but the same issue exists. What do you want to do with the TextRange? Answering that might point in the direction of a solution.

- Mark

Likes

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

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.

Likes

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
Adobe Community Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

what do you mean by "saving the script"? please elaborate, it's not clear what you're trying to do

Likes

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