3

We are using the SBSendEmail Apple code to create an email to be sent by email app.

MailApplication *mail = [SBApplication       applicationWithBundleIdentifier:@"com.apple.Mail"];

mail.delegate = self;

MailOutgoingMessage *emailMessage = [[[mail    classForScriptingClass:@"outgoing message"] alloc] initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[self.subjectField stringValue], @"subject",
[[self.messageContent textStorage] string], @"content",
[self.fromField stringValue], @"sender", 
nil]];

[[mail outgoingMessages] addObject: emailMessage];

MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[self.toField stringValue], @"address",
nil]];

[emailMessage.toRecipients addObject: theRecipient];
[emailMessage send];

Getting this error:

[General] *** -[SBProxyByCode setSender:]: object has not been added to a container yet; selector not recognized [self = 0x600000c85bf0]

Any help with getting this working or an alternate solution would be greatly appreciated!

Thanks John

John
  • 538
  • 4
  • 18
  • Did you compare your code to the SBSendEmail Apple code? The sample code sets the sender after `[[mail outgoingMessages] addObject: emailMessage];`. – Willeke Aug 04 '19 at 14:27
  • Thanks for the response Willeke. Have spent hours trying it every which way. Filed a TPS two days ago, no response. Not sure there are any macOS techs left at Apple! Works perfe – John Aug 16 '19 at 17:17
  • Any updates yet? This is causing crashes in my app. My code looks just like yours. – Dov Aug 29 '19 at 13:01
  • I've reported this bug to Apple using the Feedback Assistant, since I'm able to reproduce it in a stripped down demo project on the latest Mac and Xcode betas. – Dov Sep 03 '19 at 21:38
  • Check out the answer to this post, it works: – John Sep 03 '19 at 21:56

2 Answers2

3

Starting with Mojave, you need to provide an explanation to the user of why you're asking for AppleScript access. To do so, add this to your Info.plist:

<key>NSAppleEventsUsageDescription</key>
<string>MyApp needs to control ___ because ___</string>

Daniel Jalkut wrote a good blog post about this while Mojave was in beta.

Dov
  • 15,530
  • 13
  • 76
  • 177
0

Here is the answer from ATS:

The Scripting Bridge sample you pointed to has not been updated in several years, and is now archived. It likely has several isues that prevent it from working as originally designed, and there are no current plans to update it to work with the latest operating systems.

Rather than using scripting bridge, I recommend using Applescript directly, for example, this code:

set theSubject to "Some subject"
set theContent to "Some content of the email"
set recipientName to "Some Name"
set recipientAddress to "someone@example.com"

tell application "Mail"

# Create an email
set outgoingMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    # Set the recipient
    tell outgoingMessage
    make new to recipient with properties {name:recipientName, address:recipientAddress}

    # Send the message
    send

    end tell
end tell

You can embed this code and call it from your app using the examples described in this Apple Developer Forums post:

https://forums.developer.apple.com/message/301006#301006

John
  • 538
  • 4
  • 18
  • Check out the answer to this post, it works: The forum example referenced, although marked as correct, does not work or create an email. Perhaps a Swift version issue? – John Sep 03 '19 at 21:58
  • I recommend you accept the answer by @Dov below. It IS the answer. Security model changed in Mojave to make OSA scripting always require user authorization first, to avoid weird fancy drive-by stuff, but still let determined people get things done. – uchuugaka Dec 16 '19 at 02:24