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
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
...
Copy link to clipboard
Copied
I need this symbol to represent in code instead of
Copy link to clipboard
Copied
Try using the Unicode in your string. If you're using extendscript, "\u000A" for linefeed.
- Mark
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.
Copy link to clipboard
Copied
inserting a forced line break manually also counts as two paragraphs, how do you know it should be one?
Copy link to clipboard
Copied
In Indesign, when we apply forced line break manually the paragraph count gives one.
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.
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.
Copy link to clipboard
Copied
Thank you, Carlos Can you brief this in detail may be through a script if you can
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));
Copy link to clipboard
Copied
Thank you for the script, Carlos
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.
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.
Copy link to clipboard
Copied
please let me see the whole script you're using
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++)
{}
Copy link to clipboard
Copied
I tested your code and it works as expected. What result do you get? - Mark
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
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
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
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.
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
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.
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
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.
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