-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSDocumentController+SSYFixLaunchServicesBug.m
58 lines (50 loc) · 2.08 KB
/
NSDocumentController+SSYFixLaunchServicesBug.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
#import "NSDocumentController+SSYFixLaunchServicesBug.h"
@implementation NSDocumentController (SSYFixLaunchServicesBug)
- (NSString*)fixLaunchServicesBugForUrl:(NSURL*)url
otherUrl:(NSURL* _Nullable)otherUrl
typeName_p:(NSString**)typeName_p {
NSString* result = nil;
if (!url) {
url = otherUrl;
}
if (url) {
NSString* revisedTypeName = nil;
NSString* extension = url.absoluteString.pathExtension;
extension = [extension stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
SEL mainAppBundleSelector = NSSelectorFromString(@"mainAppBundle");
/* -mainAppBundle is defined in NSBundle+MainApp.h */
NSBundle* bundle;
if ([NSBundle respondsToSelector:mainAppBundleSelector]) {
bundle = [NSBundle performSelector:mainAppBundleSelector];
} else {
bundle = [NSBundle mainBundle];
}
NSArray* docInfos = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"];
for (NSDictionary* info in docInfos) {
for (NSString* aExtension in [info objectForKey:@"CFBundleTypeExtensions"]) {
if ([extension isEqualToString:aExtension]) {
NSString* aType = [[info objectForKey:@"LSItemContentTypes"] firstObject];
if (aType) {
revisedTypeName = [aType lowercaseString];
break;
}
}
}
if (revisedTypeName) {
break;
}
}
if (revisedTypeName && typeName_p) {
if (![revisedTypeName isEqualToString:*typeName_p]) {
result = [NSString stringWithFormat:
@"Launch Services Bug: doc type %@ (was %@) for %@",
revisedTypeName,
*typeName_p,
url.absoluteString];
*typeName_p = revisedTypeName;
}
}
}
return result;
}
@end