7a7bd6378d7a6287ff4b7a60cd60002637328b7c
[enjoyable.git] / Categories / NSRunningApplication+NJPossibleNames.m
1 //
2 // NSRunningApplication+NJPossibleNames.m
3 // Enjoyable
4 //
5 // Created by Joe Wreschnig on 3/8/13.
6 //
7 //
8
9 #import "NSRunningApplication+NJPossibleNames.h"
10
11 @implementation NSRunningApplication (NJPossibleNames)
12
13 - (NSArray *)windowTitles {
14 NSMutableArray *titles = [[NSMutableArray alloc] initWithCapacity:4];
15 NSArray *windows = CFBridgingRelease(CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID));
16 for (NSDictionary *props in windows) {
17 NSNumber *pid = props[(id)kCGWindowOwnerPID];
18 if (pid.longValue == self.processIdentifier && props[(id)kCGWindowName])
19 [titles addObject:props[(id)kCGWindowName]];
20 }
21 return titles;
22 }
23
24 - (NSString *)frontWindowTitle {
25 return self.windowTitles[0];
26 }
27
28 - (NSArray *)possibleMappingNames {
29 NSMutableArray *names = [[NSMutableArray alloc] initWithCapacity:4];
30 if (self.bundleIdentifier)
31 [names addObject:self.bundleIdentifier];
32 if (self.localizedName)
33 [names addObject:self.localizedName];
34 if (self.bundleURL)
35 [names addObject:[self.bundleURL.lastPathComponent stringByDeletingPathExtension]];
36 if (self.executableURL)
37 [names addObject:self.executableURL.lastPathComponent];
38 if (self.frontWindowTitle)
39 [names addObject:self.frontWindowTitle];
40 return names;
41 }
42
43 - (NSString *)bestMappingName {
44 // A number of Flash applications all use the generic Flash bundle
45 // ID and localized name, but they name their bundle file and
46 // executable correctly. Don't want to fall back to those IDs
47 // unless we absolutely have to.
48 NSArray *genericBundles = @[
49 @"com.macromedia.Flash Player Debugger.app",
50 @"com.macromedia.Flash Player.app",
51 ];
52 NSArray *genericExecutables = @[ @"wine.bin" ];
53 BOOL probablyWrong = ([genericBundles containsObject:self.bundleIdentifier]
54 || [genericExecutables containsObject:self.localizedName]);
55 if (!probablyWrong && self.localizedName)
56 return self.localizedName;
57 else if (!probablyWrong && self.bundleIdentifier)
58 return self.bundleIdentifier;
59 else if (self.bundleURL)
60 return [self.bundleURL.lastPathComponent stringByDeletingPathExtension];
61 else if (self.frontWindowTitle)
62 return self.frontWindowTitle;
63 else if (self.executableURL)
64 return self.executableURL.lastPathComponent;
65 else if (self.localizedName)
66 return self.localizedName;
67 else if (self.bundleIdentifier)
68 return self.bundleIdentifier;
69 else {
70 return NSLocalizedString(@"@Application",
71 @"Magic string to trigger automatic "
72 @"mapping renames. It should look like "
73 @"an identifier rather than normal "
74 @"word, with the @ on the front.");
75 }
76 }
77
78 @end