LaunchServices is the OS X API that contains info about the user's preferred applications.
The LSGetApplicationForURL()
function will return the data you seek. Here's a short example of its use:
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>
int main(int argc, char *argv[])
{
CFURLRef mailURL = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("mailto://"), NULL);
CFURLRef mailAppURL = NULL;
OSStatus ret = 0;
if((ret = LSGetApplicationForURL(mailURL, kLSRolesAll, NULL, &mailAppURL)) == 0)
{
CFStringRef path = CFURLCopyFileSystemPath(mailAppURL, kCFURLPOSIXPathStyle);
CFShow(path);
CFRelease(path);
CFRelease(mailAppURL);
}
else
{
fprintf(stderr, "LaunchServices error %d\n", ret);
}
CFRelease(mailURL);
return ret;
}
On my system, that prints /Applications/Mail.app
. If you want more info about the returned item, you can use the LSCopyItemInfoForURL()
function on mailAppURL
.