Encoding for email with NSSharingServiceMBS

Using NSSharingservice to send an email with Mail is super easy:

'prepare the body
theBody = ConvertEncoding(theBody, Encodings.ISOLatin1)
dim BodyAttributed as new NSAttributedStringMBS
call BodyAttributed.initWithHTML(theBody)
Dim items As New NSSharingServiceItemsMBS
items.AddAttributedString(BodyAttributed)

'make email
Dim service As New NSSharingServiceMBS("com.apple.share.Mail.compose")
service.subject = theSubject
service.setRecipients theFromList.Concat(theCCList)
service.performWithItems items

According to the docs I need to use ISOLatin1. But what do I do with - say - Japanese in the message body?

Email from NSSharingservice:

Original email:

Interesting question.

If you assign the attributed string to a TextArea via NSTextViewMBS, does it show correctly?

Maybe you need to set encoding with NSCharacterEncodingDocumentAttribute attribute?
UTF-8 encoding is value 4 there, I think.

Thanks, I’ll have a look.

I’m sorry. Neither your documentation nor Apple’s contain any hint how to use the shared method. This is like from a Greek philosopher:

The value of this attribute is an NSNumber object containing integer specifying NSStringEncoding for the file; default for plain text is the default encoding. This key in options can specify the string encoding for reading the data. Upon return, the document attributes can contain the actual encoding used. For writing methods, this value is used for generating the plain text data.

NSCharacterEncodingDocumentAttribute is the name of the attribute. Our shared method returns whatever Apple uses as name currently.

Which you add to the NSMutableAttributedStringMBS with addAttribute method.

Got some code working but the encoding is not okay:

'prepare the body
dim BodyAttributed as new NSAttributedStringMBS
call BodyAttributed.initWithHTML(theBody)
dim m as NSMutableAttributedStringMBS = BodyAttributed.mutableCopy
dim theRange as new NSRangeMBS(theBody)
m.addAttribute(BodyAttributed.NSCharacterEncodingDocumentAttribute, 4, theRange)
BodyAttributed = m.copy
Dim items As New NSSharingServiceItemsMBS
items.AddAttributedString(BodyAttributed)

'make email
Dim service As New NSSharingServiceMBS("com.apple.share.Mail.compose")
service.subject = theSubject
service.setRecipients theFromList.Concat(theCCList)
service.performWithItems items

Okay, seems like we need a new variant there:

initWithHTML(data as memoryblock, options as Dictionary, byref documentAttributes as dictionary) as boolean

and then passing the encoding as option finally works:

Dim theBody As String = "<html><body>Test äöü</body></html>"

Dim documentAttributes As Dictionary
Dim documentOptions As New Dictionary

// define the text encoding
Const NSUTF8StringEncoding = 4
documentOptions.Value(NSAttributedStringMBS.NSCharacterEncodingDocumentOption) = NSUTF8StringEncoding
// and a default base URL for finding images
documentOptions.Value(NSAttributedStringMBS.NSBaseURLDocumentOption) = NSURLMBS.URLWithString("https://monkeybreadsoftware.de/")
// and a timeout for network queries
documentOptions.Value(NSAttributedStringMBS.NSTimeoutDocumentOption) = 30

Dim BodyAttributed As New NSAttributedStringMBS
If BodyAttributed.initWithHTML(theBody, documentOptions, documentAttributes) Then
  MessageBox BodyAttributed.Text
Else
  Break // failed?
End If

Seems to work here. Please try tomorrow when I rebuild all the plugins for pr1.

2 Likes

Thanks, with the new plugin version the encoding is now fine.

What does this line of code do?

set the base URL for any referenced CSS or image file.
Referenced with a relative URL.