GetHelper.m
Tue May 26, 2009 · 151 words

I needed a reliable way to find out a user's default email client. After being fed up with monkey around with launchservice defaults and awk, I turned to XCode, and wrote this Foundation tool:

# GetHelper.m
# Return the helper app for a URL scheme
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{    
  if (argc < 2) {
    printf("Usage: GetHelper scheme");
    return 1;
  }

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * s = [NSString stringWithCString:argv[1]];

id helper;
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

[defaults addSuiteNamed:@"com.apple.launchservices"];
NSArray * helpers = [defaults objectForKey:@"LSHandlers"];
NSEnumerator * helperEnum = [helpers objectEnumerator];

while (helper = [helperEnum nextObject]) {
  NSString * key = [helper objectForKey:@"LSHandlerURLScheme"];
  if ([key isEqualToString:s]) {
    printf([[helper objectForKey:@"LSHandlerRoleAll"] UTF8String]);
  }
}

[pool drain];
return 0;

}

Which gives me:

./GetHelper mailto
com.apple.mail

It seems a bit overkill, but it works and eventually wasn't all that hard to put together.


back · essays · credits ·