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

remove empty lines from an array

New Here ,
Jun 04, 2019 Jun 04, 2019

Copy link to clipboard

Copied

Hi everyone

im trying to make a script that splits text into individual lines,

then duplicates a comp according to the number of lines and replaces the text in each comp (one line in each comp)

ive done the splitting part and its working fine

but i want to get rid of blank lines so i wont get a comp with an empty text layer.

turn this-

1

2

3

4

into-

1

2

3

4

heres my function

function makesupers() {

      app.beginUndoGroup("SUPER MASTER");

            var myComp;

                for (var i = 1; i <= app.project.numItems; i ++) {

                    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === 'Name Super')) {

                        var myComp = app.project.item(i);

                        var text = myPanel.grp.groupOne.Text1.text;

                        var lines = text.split(/[\n\r]/);

                            for (var l = 0; l<= lines.length; l++){

        myComp.layer("TEXT").property("ADBE Text Properties").property("ADBE Text Document").setValue(lines);

              myComp.duplicate();

              }

        break;

        app.endUndoGroup();

i tried using the .replace() method but i cant get it to work after the.split function

any suggestions?

Thanks!!

TOPICS
Scripting

Views

707

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

Enthusiast , Jun 05, 2019 Jun 05, 2019

Don't you just need something like this in your second for loop?

i.e. if lines isn't blank, do stuff

if (lines != "") {

Votes

Translate

Translate
Explorer ,
Jun 04, 2019 Jun 04, 2019

Copy link to clipboard

Copied

Try inserting the following replace command after the var text = myPanel.grp.groupOne.Text1.text; line

text = text.replace(/(\r\n|\n|\r)/gm,"");

and then you can simplify the split command to this:

var lines = text.split(" ");

Cheers!

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

Hi TheodorosGR,

thanks for your reply!!

unfortunately this doesnt work for me ..

the empty lines are erased,

but it makes a one long line of text..

i think this is because i need to first split the text to lines and then check for the blank ones

otherwise it unifies the lines because there are no spaces due to .replace

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
Explorer ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

So let's go through it step-by-step.

The .split command uses a separator (in our case we use the space " ") to split a string into an array of substrings.

Is you run the code below you will get an alert window that shows "4".

var myText = "this is a test";

var lines = myText.split(" ");

alert(lines.length);

Now, that in theory should solve your problem, but I noticed that you are trying to read \r - carriage returns and \n - line feeds in your regex function.

This leads me to believe that your myPanel.grp.groupOne.Text1.text; somehow contains these types of characters and these are the characters that give you the extra empty lines.

So by striping away the carriage returns and the line feeds you end up with a "clean" string that the split command can use.

Can you please try the code below, inside you script, and report back the result?

var text = myPanel.grp.groupOne.Text1.text;

text = text.replace(/(\r\n|\n|\r)/gm,"");

var lines = text.split(" ");

alert(lines.length);

Cheers.

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

It returns “1” .. i still get one line with all the text

i will try to explain myself a bit better

Im copying text from outside source into ae textarea.

the text copied includes the empty lines

which im trying to get rid of.

my text looks something like-

name last name, manager

another name, designer

and so on.. so im seperating this text into lines so i get each name in one comp with .split /r /n so it splits by line breaks (enter key) and it works great except for the lines that turn into a comp with empty text layer..

thats what im trying to solve

thank you very much for the help! Much appreciated!

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
Enthusiast ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

Don't you just need something like this in your second for loop?

i.e. if lines isn't blank, do stuff

if (lines != "") {

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

LATEST

So simple!! It worked!!

thank you both so much!

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
Explorer ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

Can you please try this, I've changed the 2nd line from this /gm,"" to this /gm," "

var text = myPanel.grp.groupOne.Text1.text;

text = text.replace(/(\r\n|\n|\r)/gm," ");

var lines = text.split(" ");

alert(lines.length);

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