John
John

Reputation: 548

Scripting Bridge for Send Email - Error: Object not added to container yet

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

Upvotes: 3

Views: 430

Answers (2)

Dov
Dov

Reputation: 16166

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.

Upvotes: 3

John
John

Reputation: 548

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 "[email protected]"

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

Upvotes: 0

Related Questions