2

I need to get all installed application on the OSX using objective c

I googled and found someone suggesting to do this with terminal command:

I am running this command on terminal

"system_profiler SPApplicationsDataType -xml"

to find all applications but I am not able to retrieve the terminal data from code behind.

please provide a way to fetch all installed applciations or at least please let me know to get terminal command result from objective c.

tons of thanks in advance

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

1 Answers1

5

You could use the NSFileManager Class to list all the item from the /Applications directory.

NSArray *urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationDirectory inDomains:NSLocalDomainMask];

NSError *error = nil;
NSArray *properties = [NSArray arrayWithObjects: NSURLLocalizedNameKey,
                       NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey, nil];

NSArray *array = [[NSFileManager defaultManager] 
                 contentsOfDirectoryAtURL:[urls objectAtIndex:0]
               includingPropertiesForKeys:properties
                                  options:(NSDirectoryEnumerationSkipsHiddenFiles)
                                    error:&error];
if (array == nil) {
    // Handle the error
}

Apple Doc Reference:

NSFileManager Class https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/doc/uid/20000305-SW24

File System Programming Guide https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW25