Skip to content

Instantly share code, notes, and snippets.

@gcox
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcox/9284624 to your computer and use it in GitHub Desktop.
Save gcox/9284624 to your computer and use it in GitHub Desktop.
NSFileManager category for getting a list of all applications
#import <Foundation/Foundation.h>
@interface NSFileManager (ApplicationList)
-(NSArray *)applicationList;
@end
#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