// // NGAMailMessage.m // P4Menu // // Created by Michael Bishop on 12/12/11. // Copyright (c) 2011 Numerical Garden LLC. All rights reserved. // #import "NGAMailMessage.h" #import "Mail.h" ///< Generated via the sdef and sdp commands @implementation NGAMailMessage @synthesize sendingEmailAddress; @synthesize recipients; @synthesize contents; @synthesize subject; @synthesize attachments; /* Part of the SBApplicationDelegate protocol. Called when an error occurs in Scripting Bridge method. */ - (id)eventDidFail:(const AppleEvent *)event withError:(NSError *)error { [[NSAlert alertWithMessageText:@"Error" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat: @"%@", [error localizedDescription]] runModal]; return nil; } -(IBAction)openInMail:(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.subject, @"subject", self.contents, @"content", nil]]; /* Handle a nil value gracefully. */ if(!emailMessage) return; /* add the object to the mail app */ [[mail outgoingMessages] addObject: emailMessage]; /* set the sender, show the message */ if ( self.sendingEmailAddress ) emailMessage.sender = self.sendingEmailAddress; emailMessage.visible = YES; for (NSString * recipientEmailAddress in self.recipients) { /* create a new recipient and add it to the recipients list */ MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: recipientEmailAddress, @"address", nil]]; /* Handle a nil value gracefully. */ if(!theRecipient) continue; [emailMessage.toRecipients addObject: theRecipient]; [theRecipient release]; } for (NSURL * attachmentURL in self.attachments ) { if ( ![attachmentURL isFileURL] ) continue; MailAttachment *theAttachment; /* In Snow Leopard, the fileName property requires an NSString representing the path to the * attachment. In Lion, the property has been changed to require an NSURL. */ SInt32 osxMinorVersion; Gestalt(gestaltSystemVersionMinor, &osxMinorVersion); /* create an attachment object */ if(osxMinorVersion >= 7) theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: attachmentURL, @"fileName", nil]]; else /* The string we read from the text field is a URL so we must create an NSURL instance with it * and retrieve the old style file path from the NSURL instance. */ theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: [attachmentURL path], @"fileName", nil]]; /* Handle a nil value gracefully. */ if(!theAttachment) continue; /* add it to the list of attachments */ [[emailMessage.content attachments] addObject: theAttachment]; [theAttachment release]; } /* send the message */ // [emailMessage show]; [emailMessage release]; [mail activate]; } @end