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