Setting default application for given file extension on Mac OS X from code

非 Y 不嫁゛ 提交于 2019-11-28 19:56:55

Here is a snippet of code for a very related task: set yourself as the default application for a given file extension:

#import <ApplicationServices/ApplicationServices.h>
#import "LaunchServicesWrapper.h"

@implementation LaunchServicesWrapper


+ (NSString *) UTIforFileExtension:(NSString *) extension {
    NSString * UTIString = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
                                                                       (CFStringRef)extension, 
                                                                       NULL);

    return [UTIString autorelease];
}

+ (BOOL) setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
    OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType (
                                                                   (CFStringRef) [LaunchServicesWrapper UTIforFileExtension:fileExtension],
                                                                   kLSRolesAll,
                                                                   (CFStringRef) [[NSBundle mainBundle] bundleIdentifier]
                                                                   );

    if (returnStatus != 0) {
        NSLog(@"Got an error when setting default application - %d", returnStatus);
        // Please see the documentation or LSInfo.h

        return NO;
    }

    return YES;
}


@end

Here’s a slightly modified and ARC-compliant version of Guillaume’s solution:

#import <Foundation/Foundation.h>

@interface LaunchServicesWrapper : NSObject

+ (BOOL)setMyselfAsDefaultApplicationForFileExtension:
  (NSString *)fileExtension;

@end


#import <ApplicationServices/ApplicationServices.h>
#import "LaunchServicesWrapper.h"

@implementation LaunchServicesWrapper

+ (NSString *)UTIforFileExtension:(NSString *)extension
{
  return (NSString *)CFBridgingRelease(
    UTTypeCreatePreferredIdentifierForTag(
      kUTTagClassFilenameExtension, (__bridge CFStringRef)extension,
      NULL
    )
  );
}

+ (BOOL)setMyselfAsDefaultApplicationForFileExtension:
  (NSString *)fileExtension
{
  return LSSetDefaultRoleHandlerForContentType(
    (__bridge CFStringRef) [LaunchServicesWrapper
    UTIforFileExtension:fileExtension], kLSRolesAll,
    (__bridge CFStringRef) [[NSBundle mainBundle]
    bundleIdentifier]
  );
}

@end
- (void) setApplication:(NSString *)applicationName forExtension:(NSString *)extension {

    NSArray *appPaths = [self getApplicationListForExtension:extension];

    for (NSString *appPath in appPaths) {
        if ([appPath rangeOfString:applicationName].location != NSNotFound) {
            NSArray *UTIs = (NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension,
                                                                        (CFStringRef)extension,
                                                                        nil);
            for (NSString *UTI in UTIs) {
                LSSetDefaultRoleHandlerForContentType((CFStringRef)UTI,
                                                      kLSRolesEditor,
                                                      (CFStringRef)[[NSBundle bundleWithPath:appPath] bundleIdentifier]);
            }

            [UTIs release];

            break;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!