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

Remove carriage returns and format text

Engaged ,
Mar 03, 2016 Mar 03, 2016

So I have the following code that will look at a specific layer....ignore textFrames that don't have a dash in it and then create a report txt on my desktop.

#target illustrator

var doc = app.activeDocument;

var allText = doc.textFrames;

var allLayers = doc.layers;

var count = 0;

var dashFrames = new Array();

for (z = 0; z < allLayers.length; z++) {

    if (allLayers.name == "Single Line" || allLayers.name == "Two Line" || allLayers.name == "Three Line" || allLayers.name == "Four Line") {

        //alert("Found single two three four layer");

        for (i = 0; i < allText.length; i++) {

            if (allText.typename == "TextFrame") {

                //alert(allText.layer);

                if (allText.layer != "[Layer Single Line]") {

                    //skip it

                } else {

                    if (allText.contents.match("-")) {

                        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {

                            //skip it

                        } else {

                            dashFrames[count] = allText.contents;

                            count++;

                        }

                    }

                }

            }

        }

    }

}

var formatSerials = "";

var advanceNum = 1;

for (var i = 0; i < dashFrames.length; i++) {

    if (advanceNum < dashFrames.length) {

        formatSerials += dashFrames + ", ";

        advanceNum++;

    } else {

        formatSerials += dashFrames;

    }

}

var str = '<meta name="SN Effectivity 1" content="' + formatSerials + '">\r'

var f = File("~/Desktop/" + "SN Info.txt");

f.open('w');

f.write(str);

f.close(

);

So the content of my txt file looks like this.......

<meta name="SN Effectivity 1" content="C18:

LNB1-UP

LZB1-UP

L8D1-UP

LXK1-UP, C15:

CE51-UP

LNA1-UP

LZA1-UP

L8B1-UP

LXJ1-UP, C13:

CE31-UP

NH31-UP

DH41-UP

NH41-UP">

First off it is pulling in text that doesn't have a dash (because they are part of a multiple line single text box)?

I have 3 text boxes and here is what they look like....

TEXT FRAME 1

A13:

CE31-UP

NH31-UP

DH41-UP

NH41-UP

TEXT FRAME 2

A15:

CE51-UP

LNA1-UP

LZA1-UP

L8B1-UP

LXJ1-UP

TEXT FRAME 3

A18:

LNB1-UP

LZB1-UP

L8D1-UP

LXK1-UP

So when all is said and done I need my txt output to look like this.....

<meta name="SN Effectivity 1" content="LNB1-UP, LZB1-UP, L8D1-UP, LXK1-UP, CE51-UP, LNA1-UP, LZA1-UP, L8B1-UP, LXJ1-UP, CE31-UP, NH31-UP, DH41-UP, NH41-UP">

Leave out anything without a dash.....all on one line.....separated by a comma and a space.

Any help would be greatly appreciated!

TOPICS
Scripting
1.8K
Translate
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

Valorous Hero , Mar 04, 2016 Mar 04, 2016

Then try to replace this line:

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (", ")  + '">\r';  

With this:

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (",").replace(/,/g, ", ")  + '">\r';  

Translate
Adobe
Valorous Hero ,
Mar 03, 2016 Mar 03, 2016

Try to change this line:

dashFrames[count] = allText.contents;

With this:

dashFrames.concat(allText.contents.split(/[\n\r]/g));

Translate
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
Engaged ,
Mar 03, 2016 Mar 03, 2016

When I try changing the line as you suggested it doesn't pull it into the txt file

Here is what I get in my txt output file....

<meta name="SN Effectivity 1" content="">

If I change the line to read.....

dashFrames[count] = (allText.contents.split(/[\n\r]/));

Here is what I get in my txt output file....

<meta name="SN Effectivity 1" content="A18:,LNB1-UP,LZB1-UP,L8D1-UP,LXK1-UP, A15:,CE51-UP,LNA1-UP,LZA1-UP,L8B1-UP,LXJ1-UP, A13:,CE31-UP,NH31-UP,DH41-UP,NH41-UP">

Which is close...but still pulls in the text that doesn't have a dash in it. A18:   A15:   A13:   (keeping in mind these values before the colon will change from job to job)

Translate
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
Valorous Hero ,
Mar 03, 2016 Mar 03, 2016

Okay, what if you replace the first line always?

dashFrames[count] = (allText.contents.replace(/^.+/, "").split(/[\n\r]/));

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

Silly-V

Here is what I get if I replace the line as you suggested....

<meta name="SN Effectivity 1" content=", , ">

pixxxel schubser

I had to change the tF variable you put in to match my textFrames so here is the coded updated with that. Also thank you for the suggestion on removing the formatSerials loop and replacing with the dashFrames.join!

#target illustrator 

var doc = app.activeDocument; 

var allText = doc.textFrames; 

var allLayers = doc.layers; 

var count = 0; 

var dashFrames = new Array(); 

 

for (z = 0; z < allLayers.length; z++) { 

    if (allLayers.name == "Single Line" || allLayers.name == "Two Line" || allLayers.name == "Three Line" || allLayers.name == "Four Line") { 

        //alert("Found single two three four layer"); 

        for (i = 0; i < allText.length; i++) { 

            if (allText.typename == "TextFrame") { 

            alert(allText.typename); 

                //alert(allText.layer); 

                if (allText.layer != "[Layer Single Line]") { 

                    //skip it 

                } else { 

                    alert(allText.layer); 

                    if (!allText.contents.match (/^[URSK]ENR/) && allText.contents.match ("-")) { 

                        alert(allText.contents.match ("-")); 

                    /*   --------------------------------->    THIS is not needed

                    if (allText.contents.match("-")) {

                        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {

                            //skip it

                        } else {*/ 

                            dashFrames[count] = allText.contents; 

                            count++; 

                        //} 

                    } 

                } 

            } 

        } 

    } 

 

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (", ")  + '">\r'; 

 

var f = File("~/Desktop/" + "SN Info.txt"); 

f.open('w'); 

f.write(str); 

f.close(); 

 

alert ("final:\r"+dashFrames.join ("\r")); 

Here is what I get I run the above code....

<meta name="SN Effectivity 1" content="A18:

LNB1-UP

LZB1-UP

L8D1-UP

LXK1-UP, A15:

CE51-UP

LNA1-UP

LZA1-UP

L8B1-UP

LXJ1-UP, A13:

CE31-UP

NH31-UP

DH41-UP

NH41-UP">

So I still need it to leave out the A18  A15  A13 and separate with comma space....so my txt file output should look like this...

<meta name="SN Effectivity 1" content="LNB1-UP, LZB1-UP, L8D1-UP, LXK1-UP, CE51-UP, LNA1-UP, LZA1-UP, L8B1-UP, LXJ1-UP, CE31-UP, NH31-UP, DH41-UP, NH41-UP">

Here is a link to the test page I am working with on my Google Drive if you would like to test with my file. Keeping in mind I have stripped some things out.

Adobe Forum

Thank you both for continuing to work with me on this! I really appreciate it!

Translate
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
Valorous Hero ,
Mar 04, 2016 Mar 04, 2016

Ohkay, for mine, can you try to stick this into your line?

dashFrames[count] = allText.contents.replace(/^.[^\n\r]+/, "").replace(/^[\r\n]/,"").split(/[\n\r]/);

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

SSSOOOOOO CLOSE! Silly-V

So when I put your line in I get the following output....

<meta name="SN Effectivity 1" content="LNB1-UP,LZB1-UP,L8D1-UP,LXK1-UP, CE51-UP,LNA1-UP,LZA1-UP,L8B1-UP,LXJ1-UP, CE31-UP,NH31-UP,DH41-UP,NH41-UP">

So the only thing that isn't exactly what I am looking for is the comma and space. It is correct between the text frames....just not within the text frame content lines.

Translate
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
Valorous Hero ,
Mar 04, 2016 Mar 04, 2016

Then try to replace this line:

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (", ")  + '">\r';  

With this:

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (",").replace(/,/g, ", ")  + '">\r';  

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

NAILED IT! Silly-V‌ THANK YOU SO MUCH!!!

Here is the output....

<meta name="SN Effectivity 1" content="LNB1-UP, LZB1-UP, L8D1-UP, LXK1-UP, CE51-UP, LNA1-UP, LZA1-UP, L8B1-UP, LXJ1-UP, CE31-UP, NH31-UP, DH41-UP, NH41-UP">

Here is the final code:

#target illustrator 

var doc = app.activeDocument; 

var allText = doc.textFrames; 

var allLayers = doc.layers; 

var count = 0; 

var dashFrames = new Array();  

 

for (z = 0; z < allLayers.length; z++) { 

    if (allLayers.name == "Single Line" || allLayers.name == "Two Line" || allLayers.name == "Three Line" || allLayers.name == "Four Line") { 

        //alert("Found single two three four layer"); 

        for (i = 0; i < allText.length; i++) { 

            if (allText.typename == "TextFrame") { 

                //alert(allText.layer); 

                if (allText.layer != "[Layer Single Line]") { 

                    //skip it 

                } else { 

                    if (allText.contents.match("-")) { 

                        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") { 

                            //skip it 

                        } else { 

                          dashFrames[count] = allText.contents.replace(/^.[^\n\r]+/, "").replace(/^[\r\n]/,"").split(/[\n\r]/);

                            count++; 

                        } 

                    } 

                } 

            } 

        } 

    } 

}

 

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (",").replace(/,/g, ", ")  + '">\r';

 

 

var f = File("~/Desktop/" + "SN Info.txt"); 

f.open('w'); 

f.write(str); 

f.close();

So could you maybe explain this part of line 18 to me? I am not a programmer....I get by (with some help) 

replace(/^.[^\n\r]+/, "").replace(/^[\r\n]/,"").split(/[\n\r]/);

I understand the replace function and \n = new line? \r = carriage return? But what about the /^. and +/

Thank you again for your help and time!

Translate
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
Valorous Hero ,
Mar 04, 2016 Mar 04, 2016

Hey, I'm not too fancy with the regexps myself, as you can tell from my 1st attempt for removing the 1st line.

You can use this awesome site to test your regexps: Regex Tester - Javascript, PCRE, PHP

Here is my explanation:

/

^When a ^ caret is used in regexp, it tells to match the beginning of the subject string

.the dot is supposed to match 'all characters, expect for newline

[the stuff inside the parentheses are a 'character set' - match any character in the set.

^If the caret symbol (^) is inside the [] character set 1st, it says to match characters except what's in this set.

\nThe next-line character

\rThe return character

]Closing of the character set
+Match one or more of the character preceding this + symbol
/

I mostly just go to that site and test the matchings and then make a quick test inside the ESTK to make sure they work, which I didn't do until you said my 1st answer was not working

Translate
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 Expert ,
Mar 04, 2016 Mar 04, 2016

??? Hmmh?

Be very very carefully! The following part: allText.contents.replace(/^.[^\n\r]+/, "").replace(/^[\r\n]/,"") in line#21 could be a very dangerous part!

And is cumbersome!

This code will always remove the first line of your text frame. If the contents of this first line is a „dash line“ and not „LetterDigitDigit:\r“ (e.g. A15:) this line get lost in every case!

Also I have a question too:

Why do you re-replace this line (of my own code):

if (!allText.contents.match (/^[URSK]ENR/) && allText.contents.match ("-")) {

with this cumbersome lines:

if (allText.contents.match("-")) {   

    if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {

I wonder.

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

Sorry pixxxel schubser‌ I just realized what you did! and that I skipped it! Doh!!....

That is much more efficient than mine!

Sorry about that!  Thanks for pointing it out and replying....and helping! Like I said I am not a programmer so I appreciate when guys like you and silly-v jump in and show me things like that!

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

Is there a reason this wouldn't work with just 1 item?

I have a project that it only contains 1 text frame (instead of 3)....

The contents is this....

WTL1-101

and that is also common to have just 1 text frame like that.

So when I run the script with just one text frame in the document I get...

<meta name="SN Effectivity 1" content="">

Translate
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
Valorous Hero ,
Mar 04, 2016 Mar 04, 2016

If you have only 1 item or a case where the 1st line of the text does not need to be removed, then you have to follow the advice of the mighty pixxxel !

Translate
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
Engaged ,
Mar 04, 2016 Mar 04, 2016

Here is how I addressed the if it is just 1 line.....

if (!allText.contents.match("\r")) {

  dashFrames = allText.contents;

  var useLine = 1;

  }

  else{

  dashFrames[count] = allText.contents.replace(/^.[^\n\r]+/, "").replace(/^[\r\n]/, "").split(/[\n\r]/);

  count++;

  var useLine = 2;

  }

then....

if (useLine == 1){

  useLine = dashFrames;

  }

if (useLine == 2){

  useLine = dashFrames.join(",").replace(/,/g, ", ");

  }

then....

var str = '<meta name="SN Effectivity 1" content="' + useLine + '">\r'

var f = File("~/Desktop/" + mediaNumber + " Publish Info.html");

f.open('w');

f.write(str);

f.close();

I am sure that isn't the most efficient way to do it....but hey it works hahahaha!

Translate
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 Expert ,
Mar 04, 2016 Mar 04, 2016
LATEST

Silly-V schrieb:

… then you have to follow the advice of the mighty pixxxel !

And now …

silence …

the mighty pixxxel speaks:

#target illustrator

var doc = app.activeDocument;

var allText = doc.textFrames;

var allLayers = doc.layers;

var count = 0;

var dashFrames = new Array();

for (z = 0; z < allLayers.length; z++) {

    // This line#10 makes in combination with line#12 no sense for me - but I don't know the real document

    if (allLayers.name == "Single Line" || allLayers.name == "Two Line" || allLayers.name == "Three Line" || allLayers.name == "Four Line") {

        for (i = 0; i < allText.length; i++) {

            if (allText.layer == "[Layer Single Line]" && allText.typename == "TextFrame") {

                if (!allText.contents.match (/^[URSK]ENR/) && allText.contents.match ("-")) {

                    dashFrames[count] = allText.contents.replace(/^[A-Z\d]+:[\n\r]/,"").split (/[\n\r]/).join(", ");

                    count++;

                }

            }

        }

    }

}

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (", ")  + '">\r';

var f = File("~/Desktop/" + "SN Info.txt");

f.open('w');

f.write(str);

f.close();

Translate
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 Expert ,
Mar 03, 2016 Mar 03, 2016

Sorry, if I misunderstood, but IHMO too much loops and wrong (or better: ) to complicated way.

Perhaps this is a little bit easier for you:

#target illustrator

var doc = app.activeDocument;

var allText = doc.textFrames;

var allLayers = doc.layers;

var count = 0;

var dashFrames = new Array();

for (z = 0; z < allLayers.length; z++) {

    if (allLayers.name == "Single Line" || allLayers.name == "Two Line" || allLayers.name == "Three Line" || allLayers.name == "Four Line") {

        //alert("Found single two three four layer");

        for (i = 0; i < allText.length; i++) {

            if (allText.typename == "TextFrame") {

                //alert(allText.layer);

                if (allText.layer != "[Layer Single Line]") {

                    //skip it

                } else {

                    if (!tF.contents.match (/^[URSK]ENR/) && tF.contents.match ("-")) {

                    /*   --------------------------------->    THIS is not needed

                    if (allText.contents.match("-")) {

                        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {

                            //skip it

                        } else {*/

                            dashFrames[count] = allText.contents;

                            count++;

                        //}

                    }

                }

            }

        }

    }

}

/*   --------------------------------->    THIS is not needed

var formatSerials = "";

var advanceNum = 1;

for (var i = 0; i < dashFrames.length; i++) {

    if (advanceNum < dashFrames.length) {

        formatSerials += dashFrames + ", ";

        advanceNum++;

    } else {

        formatSerials += dashFrames;

    }

}

*/

//var str = '<meta name="SN Effectivity 1" content="' + formatSerials + '">\r'

var str = '<meta name="SN Effectivity 1" content="' + dashFrames.join (", ")  + '">\r';

var f = File("~/Desktop/" + "SN Info.txt");

f.open('w');

f.write(str);

f.close();

alert ("final:\r"+dashFrames.join ("\r"));

Have fun

Translate
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