-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathNSDocumentController+DisambiguateForUTI.m
110 lines (95 loc) · 3.7 KB
/
NSDocumentController+DisambiguateForUTI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#import "NSDocumentController+DisambiguateForUTI.h"
#import "NSBundle+MainApp.h"
#import "NSArray+SafeGetters.h"
#import "NSBundle+MainApp.h"
@implementation NSDocumentController (DisambiguateForUTI)
- (NSString*)filenameExtensionForDocumentUTI:(NSString*)uti {
NSArray* docDics = [[NSBundle mainAppBundle] objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] ;
NSString* extension = nil ;
for (NSDictionary* docDic in docDics) {
NSArray* utis = [docDic objectForKey:@"LSItemContentTypes"] ;
for (NSString* uti in utis) {
if ([uti isEqualToString:uti]) {
NSArray* extensions = [docDic objectForKey:@"CFBundleTypeExtensions"] ;
extension = [extensions firstObjectSafely] ;
break ;
}
}
if (extension) {
break ;
}
}
return extension ;
}
- (NSString*)displayNameForDocumentUTI:(NSString*)uti {
NSArray* docDics = [[NSBundle mainAppBundle] objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] ;
NSString* displayName = nil ;
for (NSDictionary* docDic in docDics) {
NSArray* utis = [docDic objectForKey:@"LSItemContentTypes"] ;
for (NSString* uti in utis) {
if ([uti isEqualToString:uti]) {
displayName = [docDic objectForKey:@"CFBundleTypeName"] ;
break ;
}
}
if (displayName) {
break ;
}
}
return displayName ;
}
- (NSDictionary *)defaultDocumentInfo {
// If this executable is located in Contents/MacOS, then -[NSDocumentController defaultType]
// will return the UTI instead of the "Name" if the document type has a UTI defined.
// NSString* uti = [self defaultType] ;
// Unfortunately, if this executable is not in Contents/MacOS, as occurs if executing
// a helper application, which is located in Contents/Helpers/, for other reasons,
// -[NSDocumentController defaultType] will return nil.
// The solution is to to dig into the mainAppBundle ourselves
NSArray* docDics = [[NSBundle mainAppBundle] objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] ;
return [docDics firstObjectSafely] ;
}
- (NSString*)defaultDocumentUTI {
NSDictionary *docInfo = [self defaultDocumentInfo] ;
NSArray* utis = [docInfo objectForKey:@"LSItemContentTypes"] ;
NSString* uti = [utis firstObjectSafely] ;
return uti ;
}
- (NSString*)defaultDocumentFilenameExtension {
NSString* uti = [self defaultDocumentUTI] ;
return [self filenameExtensionForDocumentUTI:uti] ;
}
- (NSString*)defaultDocumentDisplayName {
NSString* uti = [self defaultDocumentUTI] ;
return [self displayNameForDocumentUTI:uti] ;
}
- (Class)defaultDocumentClass {
NSString* string = [[self defaultDocumentInfo] objectForKey:@"NSDocumentClass"] ;
return NSClassFromString(string) ;
}
- (NSString*)defaultDocumentIconName {
return [[self defaultDocumentInfo] objectForKey:@"CFBundleTypeIconFile"] ;
}
- (NSImage*)defaultDocumentImage {
NSString* imageName = [self defaultDocumentIconName] ;
NSBundle* bundle = [NSBundle mainBundle] ;
NSImage* image ;
if ([bundle respondsToSelector:@selector(imageForResource:)]) {
// macOS 10.7 or later can extract an image from a icon (.icns) file.
#pragma deploymate push "ignored-api-availability" // Skip it until next "pop"
image = [bundle imageForResource:imageName] ;
#pragma deploymate pop
}
else {
// macOS 10.6
// You must provide a png file. Or maybe you could leave ofType = nil,
// but I'm not sure if an .icns would interfere with that. Definitely,
// macOS 10.6 fails to get the image if you only provide a .icns.
NSString* imagePath = [bundle pathForResource:imageName
ofType:@"png"] ;
image = [[NSImage alloc] initByReferencingFile:imagePath] ;
[image autorelease] ;
}
return image ;
}
@end