PDA

View Full Version : Cocoa/Objective-C: How to get a users default email app?




mpemburn
Jun 12, 2009, 08:51 AM
Hi,

After doing some searching around, I'm not finding out how to detect which email client is the user's default. Any guidance in this?

Thanks in advance,

Mark



kainjow
Jun 12, 2009, 11:32 PM
Use LSGetApplicationForURL():

NSURL *mailtoURL = [NSURL URLWithString:@"mailto:"];
CFURLRef emailURL = NULL;
LSGetApplicationForURL((CFURLRef)mailtoURL, kLSRolesAll, NULL, &emailURL);
NSLog(@"email url: %@", [(NSURL *)emailURL autorelease]);
Make sure you add ApplicationServices.framework to your project.

mpemburn
Jun 13, 2009, 06:05 AM
Cool -- Thanks!

I just gave your code snippet a try and it returned:

email url: file://localhost/Applications/Thunderbird.app/

. . . which is spot on.

So, next I have to look into how to use this to open a new email message and hand it an attachment file.

-- Mark

mpemburn
Jun 13, 2009, 09:12 AM
Hmmm. I'm beginning to wonder if this can be done. This code:


CFURLRef mtURL = CFURLCreateWithString(kCFAllocatorDefault,CFSTR("mailto:someone@email.com"), NULL);
LSOpenCFURLRef(mtURL,nil);


. . . will open a new message with "someone@email.com" in the "TO" line. Supposedly, adding "?attachment=" to this will add an attachment but I'll be darned if I can come up with a method that will actually do that. For the most part, it does nothing.

The other possibility is to use the information returned by "LSGetApplicationForURL" to get a path to the app, craft specific command line arguments for each email client, then use NSTask to launch it. Seems like this could be involve lot of work.

-- Mark

kainjow
Jun 13, 2009, 12:11 PM
Adding an attachment in Mail via AppleScript (Scripting Bridge) wouldn't be that hard I imagine. Do you have to do this in the user's client? Maybe you could provide your own server to do the emailing instead.

mpemburn
Jun 14, 2009, 05:04 AM
Maybe some kind of AppleScript is my answer. I don't know a thing about AppleScript but I'll look into it.

This is strictly a local application. It's for document scanning and I want it to be able to send the scanned doc directly into email as well as into folders, etc.

One thing that indicates that this is not an easy proposition is my comparison of apps on the Mac and Windows platforms. The "ScanGear Toolbox" that my old Canon N670U uses is available for both platforms and while the Windows version lists all of my installed email clients for its 'scan to email' function, the Mac version does not. Also, one of my customers uses a Fujitsu scanner and, while it's "ScanSnap" software does offer an email option, it apparently will only work with Mac Mail (and, of course, he uses something else).

-- Mark

MacDonaldsd
Jun 14, 2009, 07:28 AM
Use LSGetApplicationForURL():

NSURL *mailtoURL = [NSURL URLWithString:@"mailto:"];
CFURLRef emailURL = NULL;
LSGetApplicationForURL((CFURLRef)mailtoURL, kLSRolesAll, NULL, &emailURL);
NSLog(@"email url: %@", [(NSURL *)emailURL autorelease]);
Make sure you add ApplicationServices.framework to your project.

Nice bit of code :)

quinntaylor
Jun 21, 2012, 04:43 PM
I personally find this a bit easier than using LaunchServices, and it plays nicely with ARC since there's no casting of CF types.


NSURL *emailAppURL = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:[NSURL URLWithString:@"mailto:"]];


If you create a full mailto: URL, you can use -[NSWorkspace openURL:] to open it.

Mistake
Mar 15, 2013, 01:20 AM
Use LSGetApplicationForURL():

NSURL *mailtoURL = [NSURL URLWithString:@"mailto:"];
CFURLRef emailURL = NULL;
LSGetApplicationForURL((CFURLRef)mailtoURL, kLSRolesAll, NULL, &emailURL);
NSLog(@"email url: %@", [(NSURL *)emailURL autorelease]);
Make sure you add ApplicationServices.framework to your project.

I build the code and it works good.Now i am more curious about getting the version of mail client?
How should i proceed?
Thanks

Mistake
Mar 15, 2013, 07:46 AM
I build the code and it works good.Now i am more curious about getting the version of mail client?
How should i proceed?
Thanks

I got it
NSDictionary *nsAppInfo;
NSBundle *nsAppBundle;
NSString *nsAppPath;

NSWorkspace *nsSharedWorkspace = [NSWorkspace sharedWorkspace];
nsAppPath = [nsSharedWorkspace fullPathForApplication:appName];
nsAppBundle = [NSBundle bundleWithPath: nsAppPath];
nsAppInfo = [nsAppBundle infoDictionary];
NSLog(@"%@", [nsAppInfo objectForKey:@"CFBundleShortVersionString"]);
NSLog(@"%@", [nsAppInfo objectForKey:@"CFBundleVersion"]);

As u get the nsAppInfo dictionary you can print it and saw many system level info for use.
Thanks.