Search code examples
macoscocoansfilemanager.app

Find version of installed application in OS X


I am creating an application which collects the information of the installed applications in OS X.

I tried to find all applications installed in the Applications folder with ".app" extension

I have created a function which get me some of the information of the installed application but I am looking for more data like version, bundle id and other useful information.

Here's my method to fetch attributes:

- (NSDictionary *) attributesForFile:(NSURL *)anURI fileName
                                    :(NSString*)fileName{

    // note: singleton is not thread-safe
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *aPath = [anURI path];

    if (![fileManager fileExistsAtPath:aPath]) return nil;

    NSError *attributesRetrievalError = nil;
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
                                                             error:&attributesRetrievalError];

    if (!attributes) {
        NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
        return nil;
    }


    NSMutableDictionary *returnedDictionary =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
     [attributes fileType], @"fileType",
     [attributes fileModificationDate], @"fileModificationDate",
     [attributes fileCreationDate], @"fileCreationDate",
     [attributes fileOwnerAccountName],@"fileOwnerAccount",
     fileName,@"fileName",
     [NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
     nil];

    return returnedDictionary;
}

Solution

    1. Why are you passing both a NSURL parameter and an NSString one?

    2. You can get the info that you're looking for from the NSBundle of the app:

      NSBundle *myBundle = [NSBundle bundleWithPath:@"/Applications/SomeApp.app"];
      NSLog(@"%@", [myBundle infoDictionary]);