[Scripting] Separating single text frame into multiple by looking for hard-returns with Javascript

New Here ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

Say I have a text frame with a list of items, with each list item being on a new line (using a hard return). Is there a way I can look for those hard returns programatically? I'm using Javascript / JSX.

 

What i'm trying to achieve is to take the list of items that are contained in a single text frame, and then iterate through the whole string of text to break them down into individual list items, to then create a unique text frame for each.

 

Below is my initial attempt to look for any sort of reference to the hard return (the pilcrow icon shown when turning 'show hidden characters' on in Illustrator). Unfortunately this doesn't provide anything useful. If i were doing something similar with a page of web content i could look for tags such as <br> or <p> but with Illustrator i'm not sure how to approach it.

 

Similarly -- i can't seem to find anything appropriate to look at on the ai-scripting.docsforadobe.dev website.

 

Any help or direction would be appreciated!

 

//Get array of selected items
var s = activeDocument.selection;
//Define the text content as the first selected item from the array
var textContent = s[0].contents;

//Empty character list
var charList = [];
//Iterate through text frame and push each glyph to a list of glyphs
for (var i = 0; i < textContent.length; i++) {
    var glyph = textContent[i];
    charList.push(glyph);
}
//Print list to see if there are any references to the hard return
alert(charList);

 

 

 

 

TOPICS
Scripting , Type

Views

57

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

Advocate , Apr 05, 2022 Apr 05, 2022
In a string, a carriage return is "\r" and a new line is "\n".   You might be interested in this. 

Likes

Translate

Translate
Advocate ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

In a string, a carriage return is "\r" and a new line is "\n".  

 

You might be interested in this. 

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
New Here ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

LATEST

Thanks so much — massive help.

 

For anyone else reading; one of the comments in Femkeblanco's link contains code that works a treat (see below).

 

// select textFrame
var text1 = app.selection[0].contents;
var text2 = text1.split("\r");
for (var i = 0; i < text2.length; i++) {
    alert(text2[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