在代码中为Mac OS X上的给定文件扩展名设置默认应用程序

时间:2010-07-27 13:33:18

标签: objective-c macos cocoa launch-services

我有给定文件扩展名的应用程序列表(使用LSCopyApplicationURLsForURL)。我想在从上面的调用中选择一个应用程序时从代码更改默认文件关联。 有没有办法做到这一点?

3 个答案:

答案 0 :(得分:8)

以下是一个非常相关的任务代码片段:将自己设置为给定文件扩展名的默认应用程序:

#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

答案 1 :(得分:1)

以下是Guillaume解决方案的略微修改且符合ARC的版本:

#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

答案 2 :(得分:-2)

- (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;
        }
    }
}