4 Replies Latest reply on Oct 9, 2012 5:33 AM by thor2112

    Add ASCII 13 and 10 to file

    thor2112

      Hello,

       

      Some of our systems require the Windows CR+LF line endings.  I thought something like this might work when adding text to a file (in OS X Lion with CS6):

       

      -----

      var crlf = String.fromCharCode(13, 10);

      var txt = 'abc' + crlf + 'ABC';

       

      // write to file...

      -----

       

      But it seems to be adding only 1 ASCII 10 character. 

       

      Any ideas how to accomplish this?

       

      Thanks.

        • 1. Re: Add ASCII 13 and 10 to file
          Peter Kahrel Adobe Community Professional & MVP

          var txt = 'abc\r\nABC'

           

          Though recently there was a thread that discussed a Mac-only bug in the tagged text format that messed with cr/lf, so you may have the same problem.

           

          Peter

          • 2. Re: Add ASCII 13 and 10 to file
            thor2112 Level 1

            Hi Peter,

             

            Thanks for the code and info.  Looks like this Mac-only bug exists still.  I might try using do Script() and call the tr command in the shell to replace these characters.  Ugh...  yet another crazy work-around!

             

            Thanks again for your reply!

            • 3. Re: Add ASCII 13 and 10 to file
              [Jongware] Most Valuable Participant
              var crlf = String.fromCharCode(13, 10);
              var txt = 'abc' + crlf + 'ABC';
              
              tmpFile = File(Folder.myDocuments+"/tmp.txt");
              if (tmpFile.open("w"))
              {
                        tmpFile.encoding = "binary";
                        tmpFile.write (txt);
                        tmpFile.close();
              }
              

               

               

              No problem here. Note that this probably occurs because by default a file is opened in 'system default' mode, and if it's Mac, this automatically strips the CRs. Setting the encoding to binary ensures every single bit gets transferred the exact you want it, whether on Mac or on Windows.

               

              However, when using this method you must decide at run-time if you want to write a 'Windows' or a 'Mac' compatible text file. That is, you cannot rely on $.os to determine your local OS -- you might be running your script on a Mac and need to output a Win file, or the other way around.

              • 4. Re: Add ASCII 13 and 10 to file
                thor2112 Level 1

                Hi [Jongware],

                 

                Thank you very much for your code!  This will be so much better than the path I was headed down!

                 

                Thanks again!