16 Replies Latest reply on Mar 4, 2016 11:25 AM by pixxxel schubser

    Remove carriage returns and format text

    subieguy2 Level 1

      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[z].name == "Single Line" || allLayers[z].name == "Two Line" || allLayers[z].name == "Three Line" || allLayers[z].name == "Four Line") {
              //alert("Found single two three four layer");
              for (i = 0; i < allText.length; i++) {
                  if (allText[i].typename == "TextFrame") {
                      //alert(allText[i].layer);
                      if (allText[i].layer != "[Layer Single Line]") {
                          //skip it
                      } else {
                          if (allText[i].contents.match("-")) {
                              if (allText[i].contents.substr(0, 4) == "UENR" || allText[i].contents.substr(0, 4) == "RENR" || allText[i].contents.substr(0, 4) == "SENR" || allText[i].contents.substr(0, 4) == "KENR") {
                                  //skip it
                              } else {
                                  dashFrames[count] = allText[i].contents;
                                  count++;
                              }
                          }
                      }
                  }
              }
          }
      }
      
      
      var formatSerials = "";
      var advanceNum = 1;
      for (var i = 0; i < dashFrames.length; i++) {
          if (advanceNum < dashFrames.length) {
              formatSerials += dashFrames[i] + ", ";
              advanceNum++;
          } else {
              formatSerials += dashFrames[i];
          }
      }
      
      
      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!

        • 1. Re: Remove carriage returns and format text
          Silly-V Adobe Community Professional

          Try to change this line:

          dashFrames[count] = allText[i].contents;

           

          With this:

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

          • 2. Re: Remove carriage returns and format text
            subieguy2 Level 1

            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[i].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)

            • 3. Re: Remove carriage returns and format text
              Silly-V Adobe Community Professional

              Okay, what if you replace the first line always?

               

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

              • 4. Re: Remove carriage returns and format text
                pixxxel schubser Level 5

                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[z].name == "Single Line" || allLayers[z].name == "Two Line" || allLayers[z].name == "Three Line" || allLayers[z].name == "Four Line") {
                        //alert("Found single two three four layer");
                        for (i = 0; i < allText.length; i++) {
                            if (allText[i].typename == "TextFrame") {
                                //alert(allText[i].layer);
                                if (allText[i].layer != "[Layer Single Line]") {
                                    //skip it
                                } else {
                                    if (!tF[i].contents.match (/^[URSK]ENR/) && tF[i].contents.match ("-")) {
                                    /*   --------------------------------->    THIS is not needed
                                    if (allText[i].contents.match("-")) {
                                        if (allText[i].contents.substr(0, 4) == "UENR" || allText[i].contents.substr(0, 4) == "RENR" || allText[i].contents.substr(0, 4) == "SENR" || allText[i].contents.substr(0, 4) == "KENR") {
                                            //skip it
                                        } else {*/
                                            dashFrames[count] = allText[i].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[i] + ", ";
                        advanceNum++;
                    } else {
                        formatSerials += dashFrames[i];
                    }
                }
                */
                
                //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

                 

                • 5. Re: Remove carriage returns and format text
                  subieguy2 Level 1

                  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[z].name == "Single Line" || allLayers[z].name == "Two Line" || allLayers[z].name == "Three Line" || allLayers[z].name == "Four Line") {  
                          //alert("Found single two three four layer");  
                          for (i = 0; i < allText.length; i++) {  
                              if (allText[i].typename == "TextFrame") {  
                              alert(allText[i].typename);  
                                  //alert(allText[i].layer);  
                                  if (allText[i].layer != "[Layer Single Line]") {  
                                      //skip it  
                                  } else {  
                                      alert(allText[i].layer);  
                                      if (!allText[i].contents.match (/^[URSK]ENR/) && allText[i].contents.match ("-")) {  
                                          alert(allText[i].contents.match ("-"));  
                                      /*   --------------------------------->    THIS is not needed 
                                      if (allText[i].contents.match("-")) { 
                                          if (allText[i].contents.substr(0, 4) == "UENR" || allText[i].contents.substr(0, 4) == "RENR" || allText[i].contents.substr(0, 4) == "SENR" || allText[i].contents.substr(0, 4) == "KENR") { 
                                              //skip it 
                                          } else {*/  
                                              dashFrames[count] = allText[i].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!

                  • 6. Re: Remove carriage returns and format text
                    Silly-V Adobe Community Professional

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

                     

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

                    • 7. Re: Remove carriage returns and format text
                      subieguy2 Level 1

                      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.

                      • 8. Re: Remove carriage returns and format text
                        Silly-V Adobe Community Professional

                        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';  

                        • 9. Re: Remove carriage returns and format text
                          subieguy2 Level 1

                          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[z].name == "Single Line" || allLayers[z].name == "Two Line" || allLayers[z].name == "Three Line" || allLayers[z].name == "Four Line") {  
                                  //alert("Found single two three four layer");  
                                  for (i = 0; i < allText.length; i++) {  
                                      if (allText[i].typename == "TextFrame") {  
                                          //alert(allText[i].layer);  
                                          if (allText[i].layer != "[Layer Single Line]") {  
                                              //skip it  
                                          } else {  
                                              if (allText[i].contents.match("-")) {  
                                                  if (allText[i].contents.substr(0, 4) == "UENR" || allText[i].contents.substr(0, 4) == "RENR" || allText[i].contents.substr(0, 4) == "SENR" || allText[i].contents.substr(0, 4) == "KENR") {  
                                                      //skip it  
                                                  } else {  
                                                    dashFrames[count] = allText[i].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!

                          • 10. Re: Remove carriage returns and format text
                            subieguy2 Level 1

                            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="">

                            • 11. Re: Remove carriage returns and format text
                              Silly-V Adobe Community Professional

                              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

                              • 12. Re: Remove carriage returns and format text
                                pixxxel schubser Level 5

                                ??? Hmmh?

                                 

                                Be very very carefully! The following part: allText[i].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[i].contents.match (/^[URSK]ENR/) && allText[i].contents.match ("-")) {
                                

                                 

                                with this cumbersome lines:

                                if (allText[i].contents.match("-")) {    
                                    if (allText[i].contents.substr(0, 4) == "UENR" || allText[i].contents.substr(0, 4) == "RENR" || allText[i].contents.substr(0, 4) == "SENR" || allText[i].contents.substr(0, 4) == "KENR") {
                                

                                 

                                 

                                I wonder.

                                 

                                • 13. Re: Remove carriage returns and format text
                                  Silly-V Adobe Community Professional

                                  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 !

                                  • 14. Re: Remove carriage returns and format text
                                    subieguy2 Level 1

                                    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!

                                    • 15. Re: Remove carriage returns and format text
                                      subieguy2 Level 1

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

                                       

                                      if (!allText[i].contents.match("\r")) {
                                        dashFrames = allText[i].contents;
                                        var useLine = 1;
                                        }
                                        else{
                                        dashFrames[count] = allText[i].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!

                                      • 16. Re: Remove carriage returns and format text
                                        pixxxel schubser Level 5

                                        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[z].name == "Single Line" || allLayers[z].name == "Two Line" || allLayers[z].name == "Three Line" || allLayers[z].name == "Four Line") {
                                                for (i = 0; i < allText.length; i++) {
                                                    if (allText[i].layer == "[Layer Single Line]" && allText[i].typename == "TextFrame") {
                                                        if (!allText[i].contents.match (/^[URSK]ENR/) && allText[i].contents.match ("-")) {
                                                            dashFrames[count] = allText[i].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();