NSSharingService to create mail message

Hi all,

I’m using the code snippet kindly provided by @Jürg_Otter which works great. Does anyone here have the knowhow to include either HTML or RTF content in the mail message body (rather than plain text)?

Const NSSharingServiceNameComposeEmail = “com.apple.share.Mail.compose”

Declare Function NSClassFromString Lib “Cocoa” (className As CFStringRef) As Ptr
Declare Function sharingServiceNamed Lib “Cocoa” selector “sharingServiceNamed:” ( NSSharingServiceClass As Ptr, serviceName As CFStringRef ) As Ptr

Declare Sub setRecipients Lib “Cocoa” selector “setRecipients:” (NSSharingServiceInstance As Ptr, obj As Ptr)
Declare Sub setSubject Lib “Cocoa” selector “setSubject:” (NSSharingServiceInstance As Ptr, subject As CFStringRef)
Declare Sub performWithItems Lib “Cocoa” selector “performWithItems:” (NSSharingServiceInstance As Ptr, obj As Ptr)

'NSSharingService instance
Dim ptrNSSharingServiceClass As Ptr = NSClassFromString(“NSSharingService”)
Dim ptrNSSharingServiceComposeEmail As Ptr = sharingServiceNamed(ptrNSSharingServiceClass, NSSharingServiceNameComposeEmail)

//Declares for NSMutableArray
Declare Function alloc Lib “Cocoa” Selector “alloc” (NSClass As Ptr) As Ptr
Declare Function init Lib “Cocoa” Selector “init” (NSClass As Ptr) As Ptr
Declare Sub addObject_String Lib “Cocoa” Selector “addObject:”(NSMutableArrayClass As Ptr, anObject As CFStringRef)
Declare Sub addObject_Ptr Lib “Cocoa” Selector “addObject:”(NSMutableArrayClass As Ptr, anObject As Ptr)

Static ptrMutableArrayClass As Ptr = NSClassFromString(“NSMutableArray”)

'Build Recipients Array
var ptrRecipients As Ptr = init(alloc(ptrMutableArrayClass))
addObject_String(ptrRecipients, theRecipient)

'Build Items Array
var ptrItems As Ptr = init(alloc(ptrMutableArrayClass))
addObject_String(ptrItems, theContent)

'add Attachments to Items Array
Declare Function fileURLWithPath Lib “Foundation” selector “fileURLWithPath:” ( ptrNSURLClass As Ptr, path As CFStringRef ) As Ptr

for each oAttachFolderItem As FolderItem in theFiles
if (oAttachFolderItem = nil) or (oAttachFolderItem.Exists = false) then continue
var ptrNSURLClass As Ptr = NSClassFromString(“NSURL”)
var ptrAttachment As Ptr = fileURLWithPath(ptrNSURLClass, oAttachFolderItem.NativePath)
addObject_Ptr(ptrItems, ptrAttachment)
next

'set Subject, Recipient
setSubject(ptrNSSharingServiceComposeEmail, theSubject)
setRecipients(ptrNSSharingServiceComposeEmail, ptrRecipients)

'Compose E-Mail with Items
performWithItems(ptrNSSharingServiceComposeEmail, ptrItems)

Many thanks,
Dave.

In the example above:

'Build Items Array for Content (Body and Attachments)
Dim ptrItems As Ptr = init(alloc(ptrMutableArrayClass))
addObject_String(ptrItems, theContent)

It just adds a “String” (theContent). Instead you would need to create a NSAttributedString (again using Declares or Plugins, which aren’t part of the example that I’ve created), and add a “formatted String”.

Something along these lines:
Programmatically Sending Rich Text e-mail with Attachments on the Mac

Any chance someone could flesh out Jurg’s suggestion? tia