Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 12, 2009, 08:51 AM   #1
mpemburn
macrumors member
 
Join Date: Jan 2008
Location: Bel Air, MD USA
Cocoa/Objective-C: How to get a users default email app?

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
mpemburn is offline   0 Reply With Quote
Old Jun 12, 2009, 11:32 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Use LSGetApplicationForURL():

Code:
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.
kainjow is offline   0 Reply With Quote
Old Jun 13, 2009, 06:05 AM   #3
mpemburn
Thread Starter
macrumors member
 
Join Date: Jan 2008
Location: Bel Air, MD USA
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 is offline   0 Reply With Quote
Old Jun 13, 2009, 09:12 AM   #4
mpemburn
Thread Starter
macrumors member
 
Join Date: Jan 2008
Location: Bel Air, MD USA
Hmmm. I'm beginning to wonder if this can be done. This code:

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
mpemburn is offline   0 Reply With Quote
Old Jun 13, 2009, 12:11 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Jun 14, 2009, 05:04 AM   #6
mpemburn
Thread Starter
macrumors member
 
Join Date: Jan 2008
Location: Bel Air, MD USA
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
mpemburn is offline   0 Reply With Quote
Old Jun 14, 2009, 07:28 AM   #7
MacDonaldsd
macrumors 65816
 
MacDonaldsd's Avatar
 
Join Date: Sep 2005
Location: London , UK
Quote:
Originally Posted by kainjow View Post
Use LSGetApplicationForURL():

Code:
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
MacDonaldsd is offline   0 Reply With Quote
Old Jun 21, 2012, 04:43 PM   #8
quinntaylor
macrumors member
 
Join Date: Aug 2008
NSWorkspace supports this

I personally find this a bit easier than using LaunchServices, and it plays nicely with ARC since there's no casting of CF types.

Code:
NSURL *emailAppURL = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:[NSURL URLWithString:@"mailto:"]];
If you create a full mailto: URL, you can use -[NSWorkspace openURL:] to open it.
quinntaylor is offline   0 Reply With Quote
Old Mar 15, 2013, 01:20 AM   #9
Mistake
macrumors newbie
 
Join Date: Feb 2013
Version of default mail client?

Quote:
Originally Posted by kainjow View Post
Use LSGetApplicationForURL():

Code:
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 is offline   0 Reply With Quote
Old Mar 15, 2013, 07:46 AM   #10
Mistake
macrumors newbie
 
Join Date: Feb 2013
Quote:
Originally Posted by Mistake View Post
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
Code:
 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.

Last edited by dejo; Mar 15, 2013 at 12:29 PM. Reason: Please use [code] tags.
Mistake is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Cocoa Touch and Objective C phalgun27 iPhone/iPad Programming 7 Aug 1, 2013 07:09 PM
Basic Objective-C Cocoa questions? cpuin Mac Programming 8 Jun 3, 2013 07:10 PM
iPhone: How to make a 3rd party email app as default?? TL24 Jailbreaks and iOS Hacks 0 Mar 13, 2013 04:49 PM
Use Apple Mail app as default email app in Windows 7 Pro 00Falcon Mac Applications and Mac App Store 2 Nov 14, 2012 11:49 PM
WebView in Cocoa (Objective-C) tr!pf!3 Mac Programming 0 Aug 28, 2012 09:48 AM

Forum Jump

All times are GMT -5. The time now is 12:10 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC