Question

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;
}
Was it helpful?

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]);
    

OTHER TIPS

term> /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep '.app' | grep '.path'

Can be done with a bash script:

#!/bin/bash

cd /Applications
files=(*.app)

for file in "${files[@]}"; do
  value=`plutil -p "/Applications/$file/Contents/Info.plist" | grep CFBundleShortVersionString`
  echo "${file//.app/}: ${value##*>}" | tr -d '"'
done

Example:

Android Studio: 3.6
App Store: 3.0
AppCleaner: 3.5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top