Search code examples
objective-ccmacossystem-information

how to get default mail client version on mac os x?


I am new here. I have searched about default mail client information on mac os x. I found some help here How do I get the default mail client using applescript? But i did not got all the information i wanted.I got default mail client name but could not got its version that i see in "About Mail" section of Mail.app(when launched).


Solution

  • 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.