| Article: |
Working with Sheets in Cocoa | |
| Subject: | Extension: Sending email from Address Book | |
| Date: | 2001-10-17 15:34:16 | |
| From: | mikenidel | |
|
I've been sticking pretty closely to this tutorial so far, but I finally decided I felt intrepid enough to try extending the application a bit.
|
||
Showing messages 1 through 2 of 2.
-
Extension: Sending email from Address Book
2001-10-26 12:07:56 rainwadj [Reply | View]
This is a great addition. I figured out how to clean up the string. When the Send Mail button is clicked, I have another sheet open with fields for To: (which is filled in with the email addresses from the selected records), Cc:, Bcc: and Subject:, and buttons for Send and Cancel. The action for the Send button on the sheet invokes this method:
- (IBAction)sendEmailMessage:(id)sender
{
NSMutableString *temp = [NSMutableString
stringWithString:@"mailto:"];
NSString *url;
[temp appendString:[emailToField stringValue]];
[temp appendString:@"?cc="];
[temp appendString:[emailCcField stringValue]];
[temp appendString:@"&bcc="];
[temp appendString:[emailBccField stringValue]];
[temp appendString:@"&subject="];
[temp appendString:[emailSubjField stringValue]];
url = (NSString *)
CFURLCreateStringByAddingPercentEscapes
(NULL, (CFStringRef)temp, NULL, NULL,
kCFStringEncodingISOLatin1);
[[NSWorkspace sharedWorkspace] openURL:
[NSURL URLWithString:url]];
[NSApp stopModal];
}
Everything gets put in the proper places in a new message window in Mail.app. Cool stuff.




// For sending mail
- (IBAction) sendMail: (id) sender
{
NSMutableString *url = [NSMutableString stringWithString: @"mailto:"];
NSString *str;
NSEnumerator *enumerator;
NSNumber *index;
// Check if there is a selected row
if ([tableView numberOfSelectedRows] == 0)
{
return;
}
// Enumerate all selected rows
enumerator = [tableView selectedRowEnumerator];
// Traverse the selected rows with enumerator
while ( ( index = [enumerator nextObject] ) )
{
// Retrieve the email field
str = [[records objectAtIndex: [index intValue]]
objectForKey: @"Email"];
// Add a comma to separate email addresses if this is not the first one
if (![url isEqualToString: @"mailto:"])
{
[url appendString: @","];
}
// Add the email address to the previously built string
[url appendString: str];
}