Scripting-Bridge-for-send-email-error-object-not-added-to-container-yet

Hello,


We are in the process of porting our iOS apps to macOS. Our apps create emails with PDF attachments. Very easily accomplshed in iOS with a half-dozen or so lines of code.


We are using the SBSendEmail Apple Sample Code to create an email to be sent by email app. Which we understand, like many of the mac samples, it is 8 years old, and no longer supported.


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

Replies

Given the error message you received I assume that you are trying to set the "sender" property of a MailOutgoingMessage instance? I don't often use scripting bridge, but it sounds like the message object expects to be part of an enclosing container or hierarchy before you configure it. How did you obtain your message object? Did you create it using the SBApplication (MailApplication) instance?

Thanks for the reply. Here is the code. Although we have been doing this for seven years and have 17 iOS apps we pretty much operate in the Xcode/Objective C and occasionally the Swift world.


Here is the link to the actual project: https://developer.apple.com/library/archive/samplecode/SBSendEmail/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004645



Here is the one method in the project that does what we need. Adds a message to the outgoing messages.


- (IBAction)sendEmailMessage:(id)sender {


/* create a Scripting Bridge object for talking to the Mail application */

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


/* set ourself as the delegate to receive any errors */

mail.delegate = self;


/* create a new outgoing message object */

MailOutgoingMessage *emailMessage = [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:

[NSDictionary dictionaryWithObjectsAndKeys:

[self.subjectField stringValue], @"subject",

[[self.messageContent textStorage] string], @"content",

nil]];


/* add the object to the mail app */

[[mail outgoingMessages] addObject: emailMessage];


/* set the sender, show the message */

emailMessage.sender = [self.fromField stringValue];

emailMessage.visible = YES;


/* Test for errors */

if ( [mail lastError] != nil )

return;


/* create a new recipient and add it to the recipients list */

MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:

[NSDictionary dictionaryWithObjectsAndKeys:

[self.toField stringValue], @"address",

nil]];

[emailMessage.toRecipients addObject: theRecipient];

[theRecipient release];


/* Test for errors */

if ( [mail lastError] != nil )

return;

}

/* send the message */

[emailMessage send];


}