Saving a text file looses CR

Hi,

I’m using a TextArea for the user to enter an Email message that will be used multiple times.

The proble, is in the finished email (Client is Mac Mail) the carriage returns are lost. It comes out as 1 long string. However they show in the TextArea control?

I save it with this code:

  Dim f as FolderItem
  Dim t as TextOutputStream
  
  msEmailBody = txtBodyOfEmail.Text
  
  f=GetFolderItem("InvoiceEmail.txt")
  If f <> Nil then
    t=TextOutputStream.Create(f)
    t.Write(msEmailBody)
    t.Close
    mbNedToSaveBody = False
    me.Enabled = False
  End if

And Loaded with this code:

  Dim f as FolderItem
  Dim t As TextInputStream
  
  f=GetFolderItem("InvoiceEmail.txt")
  
  If f <> Nil and f.Exists then
    t=TextInputStream.Open(f)
    't.Encoding=Encodings.MacRoman //specify encoding of input stream
    txtBodyOfEmail.Text=t.ReadAll
    t.Close
    msEmailBody = txtBodyOfEmail.Text
    mbNedToSaveBody = False
    btnSave.Enabled = False
  End
  

I’m sure it’s something simple, but I can’t figure it out.

Thanks
Rich

I’m not sure exactly how EndOfLine characters should be handled across different platforms but the following workaround should do the trick:

In your save method, replace:

msEmailBody = txtBodyOfEmail.Text

With

msEmailBody = ReplaceAll(txtBodyOfEmail.Text, Chr(13), EndOfLine)

From what I could gather in the debugger, the textarea stores “end of lines” as a single ASCII 13 character. On some systems the ASCII 13 need to be followed by a ASCII 10 character. The replace instruction above does just that (inserts ASCII 10 characters after all the character 13 characters).

An OSX text ends with LF. If you need to save some text to be used in an email, that was encoded as MacRoman first, converting it to UTF8 before sending out is advised. As for converting the EndOfLines from LF to CR/LF, try ReplaceLineEndings (SourceString, EndOfLine.Windows)

Thanks Ricardo, there I also learning something new. ReplaceLineEndings is a much more elegant way to replace line endings.

Ricardo thanks, I’ll try it.

How do I convert it or make sure it is UTF8?

Check this: :wink:

https://documentation.xojo.com/index.php/Encoding

https://documentation.xojo.com/index.php/ConvertEncoding

Alwin, you are 100 years ahead me in Xojo. I only knew this because I am forced to learn Xojo from the 2013 edition. So, sometimes I see some novelty. :wink:

The msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows) Did not work.

I read up on the converting.

Can you share a sample of the problem? Including that text file you are loading? You can zip and let’s test this “Attach file” feature I see below.

What attach feature?

…I see below when composing the message. Hey! The edition feature vanished.

Test Attaching. Forget it. It does not work. Use a service ike this https://www.transferbigfiles.com/

like

When I use this service, in the field email, I just put “@”.

[quote=9611:@Richard Albrecht]The msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows) Did not work.

I read up on the converting.[/quote]

Are you using a Mac Richard? I think you then you might have to use…

msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Macintosh)

I just tried :

msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows).ConvertEncoding(Encodings.UTF8)

and that didn’t work.

Yes I’m using a Mac

Isn’t using EndOfLine.Macintosh converting it to Mac???

Let me try.

https://documentation.xojo.com/index.php/EndOfLine.Windows

I think it then converts it to proper line endings for the Mac.

The Problem reported by Richard was missing CR’s. He told that his text had just LF’s. As OSX does. To save a text with CR+LF, we need to replace the LF’s by CR+LF’s. We we are confused, the bast way to know is a sample. :wink: