osx

File association

Set my app as default app for a file type

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

    return [UTIString autorelease];
}

- (BOOL) setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
    OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType (
                                                                   (CFStringRef) [self 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;
}

source

Create association with new/custom file types via Info.plist

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFile</key>
        <string>Icon file for associated file</string>
        <key>CFBundleTypeName</key>
        <string>My file format</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string> <!-- The value can be Editor, Viewer, Shell, or None. This key is required.  -->
        <key>LSItemContentTypes</key>
        <array>
            <string>UTI of the file</string> <!-- Existing UTI or create a UTI for your new file type -->
        </array>
        <key>LSHandlerRank</key>
        <string>Owner</string>
    </dict>
</array>

source


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow