-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.mm
129 lines (96 loc) · 3.88 KB
/
main.mm
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@interface LSApplicationWorkspace
+ (id)defaultWorkspace;
- (id)allInstalledApplications;
@end
@interface LSBundleProxy
@property (nonatomic, readonly) NSURL *bundleContainerURL;
@property (nonatomic, readonly) NSString *bundleExecutable;
@property (nonatomic, readonly) NSString *bundleIdentifier;
@property (nonatomic, readonly) NSString *bundleType;
@property (nonatomic, readonly) NSURL *bundleURL;
@property (nonatomic, readonly) NSString *bundleVersion;
@property (nonatomic, readonly) NSURL *containerURL;
@property (nonatomic, readonly) NSURL *dataContainerURL;
@property (nonatomic, readonly) NSDictionary *entitlements;
@property (nonatomic, readonly) NSDictionary *groupContainerURLs;
@property (nonatomic, readonly) BOOL isContainerized;
@property (nonatomic, readonly) NSString *localizedShortName;
@property (nonatomic, readonly) NSString *signerIdentity;
@property (nonatomic, readonly) NSString *teamID;
@end
@interface LSApplicationProxy : LSBundleProxy
- (id)localizedName;
@end
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@interface AppUtils : NSObject
+ (instancetype)sharedInstance;
- (void) searchApp:(NSString *)name;
@end
@implementation AppUtils
static NSArray* apps;
+ (instancetype)sharedInstance
{
static AppUtils *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[AppUtils alloc] init];
apps = [[LSApplicationWorkspace defaultWorkspace] allInstalledApplications];
});
return sharedInstance;
}
- (void) searchApp:(NSString *)searchTerm
{
NSPredicate *predicateExecutable = [NSPredicate predicateWithFormat:@"bundleExecutable contains[cd] %@", searchTerm];
NSPredicate *predicateIdentifier = [NSPredicate predicateWithFormat:@"bundleIdentifier contains[cd] %@", searchTerm];
NSPredicate *nameIdentifier = [NSPredicate predicateWithFormat:@"localizedName contains[cd] %@", searchTerm];
NSArray *subPredicates = [NSArray arrayWithObjects:predicateExecutable, predicateIdentifier, nameIdentifier, nil];
NSCompoundPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
NSArray *filtered = [apps filteredArrayUsingPredicate:predicate];
int i = 1;
for (LSApplicationProxy *app in filtered)
{
NSString *teamID = app.teamID;
NSString *identifier = app.bundleIdentifier;
NSString *bundleContainer = app.bundleContainerURL.path;
NSString *dataContainer = app.dataContainerURL.path;
NSString *localizedName = app.localizedName;
NSDictionary *groupContainerURLs = app.groupContainerURLs;
if(bundleContainer == nil)
bundleContainer = app.bundleURL.path;
if(![teamID isEqualToString:@"0000000000"])
identifier = [NSString stringWithFormat:@"%@.%@", teamID, identifier];
NSLog(@"[%i] %@ (%@)", i, localizedName, identifier);
if(bundleContainer)
NSLog(@"Bundle: %@", bundleContainer);
if(dataContainer)
NSLog(@"Data: %@", dataContainer);
if(groupContainerURLs != nil && [groupContainerURLs count] > 0) {
for (id key in groupContainerURLs ) {
NSURL *groupURL = [groupContainerURLs objectForKey:key];
NSLog(@"Group: %@ (%@)\n", groupURL.path, key);
}
} else {
NSLog(@"");
}
i++;
}
}
@end
int main(int argc, char **argv, char **envp) {
if(argc < 2) {
fprintf(stderr, "Syntax: %s searchTerm\n", argv[0]);
return 1;
}
char *filter = NULL;
if (optind < argc)
{
filter = argv[optind++];
}
if(filter == NULL) {
NSLog(@"Error: Search term is missing.")
exit(0);
}
NSString* searchTerm = [NSString stringWithUTF8String:filter];
[[AppUtils sharedInstance] searchApp:searchTerm];
exit (0);
}