-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYLaunchdBasics.m
77 lines (64 loc) · 2.05 KB
/
SSYLaunchdBasics.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
#import "SSYLaunchdBasics.h"
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
#define NO_ARC 1
#else
#if __has_feature(objc_arc)
#define NO_ARC 0
#else
#define NO_ARC 1
#endif
#endif
@implementation SSYLaunchdBasics
+ (NSString*)homeLaunchAgentsPath {
return [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"LaunchAgents"] ;
}
+ (NSSet*)installedLaunchdAgentLabelsWithPrefix:(NSString*)prefix {
NSError* error = nil ;
NSArray* allAgentNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self homeLaunchAgentsPath]
error:&error] ;
if (!allAgentNames) {
if (
([error code] != NSFileReadNoSuchFileError)
||
(![[error domain] isEqualToString:NSCocoaErrorDomain])
) {
NSLog(@"Internal Error 257-8032 %@", error) ;
}
}
NSMutableSet* targetAgentNames = [[NSMutableSet alloc] init] ;
if (prefix) {
for (NSString* agentName in allAgentNames) {
if ([agentName hasPrefix:prefix]) {
[targetAgentNames addObject:[agentName stringByDeletingPathExtension]] ;
}
}
}
NSSet* answer = [targetAgentNames copy] ;
#if !__has_feature(objc_arc)
[targetAgentNames release] ;
[answer autorelease] ;
#endif
return answer ;
}
+ (NSDictionary*)installedLaunchdAgentsWithPrefix:(NSString*)prefix {
NSSet* allAgentNames = [self installedLaunchdAgentLabelsWithPrefix:prefix] ;
NSString* directory = [self homeLaunchAgentsPath] ;
NSMutableDictionary* agents = [[NSMutableDictionary alloc] init] ;
for (NSString* agentName in allAgentNames) {
NSString* filename = [agentName stringByAppendingPathExtension:@"plist"] ;
NSString* path = [directory stringByAppendingPathComponent:filename] ;
NSDictionary* agentDic = [NSDictionary dictionaryWithContentsOfFile:path] ;
// Use defensive programming when reading from files!
if ([agentDic isKindOfClass:[NSDictionary class]]) {
[agents setObject:agentDic
forKey:agentName] ;
}
}
NSDictionary* answer = [agents copy] ;
#if !__has_feature(objc_arc)
[agents release] ;
[answer autorelease] ;
#endif
return answer ;
}
@end