Last active
August 29, 2015 13:56
-
-
Save gcox/9284624 to your computer and use it in GitHub Desktop.
NSFileManager category for getting a list of all applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSFileManager (ApplicationList) | |
-(NSArray *)applicationList; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSFileManager+ApplicationList.h" | |
@implementation NSFileManager (ApplicationList) | |
-(NSArray *)applicationList { | |
NSMutableArray *applications = NSMutableArray.new; | |
NSArray *array = NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, TRUE); | |
for (NSString *path in array) | |
[applications addObjectsFromArray:[self applicationsInDirectory:path]]; | |
return applications; | |
} | |
-(NSArray *)applicationsInDirectory:(NSString *)directory { | |
NSMutableArray *array = NSMutableArray.new; | |
NSError *error = nil; | |
for (NSString *path in [self contentsOfDirectoryAtPath:directory error:&error]) { | |
if (error != nil) { | |
NSLog(@"Error found when grabbing list of applications: %@", error); | |
return array; | |
} | |
if ([path hasSuffix:@".app"]) { | |
[array addObject:path]; | |
continue; | |
} | |
BOOL isDirectory; | |
if ([self fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory) | |
[array addObjectsFromArray:[self applicationsInDirectory:path]]; | |
} | |
return array; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment