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

3

5

I have the list of the applications for given file extension (using LSCopyApplicationURLsForURL). I want to change the default file association from code upon selecting one of the applications from the above call. Is there a way to do this?

Prittleprattle answered 27/7, 2010 at 13:33 Comment(0)
M
8

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
Mckibben answered 27/12, 2011 at 13:59 Comment(2)
Thanks, but already found the answer (see my answer) - yours its a bit different from mine, since it founds only UTI for an extension. What if there are more?Prittleprattle
Sadly, this no longer works for sandboxed applications.Ambrosine
S
1

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
Sain answered 26/4, 2013 at 13:0 Comment(1)
Is this still the best way to go about this? I'm working on a mac app in Swift and I'll translate this to Swift if so.Prefer
P
-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;
        }
    }
}
Prittleprattle answered 27/7, 2010 at 14:31 Comment(3)
This code gets thisAppName but doesn't do anything with it.Pettit
getApplicationListForExtension: isn’t defined.Sain
Not defined, but it returns just an NSArray of strings.Prittleprattle

© 2022 - 2024 — McMap. All rights reserved.