Reputation: 187
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;
}
Upvotes: 4
Views: 3267
Reputation: 567
On the command line, the following extracts the value of the specified key from the plist file without needing grep
or tr
:
plutil -extract CFBundleShortVersionString raw "/Applications/$file/Contents/Info.plist"
Upvotes: 0
Reputation: 8142
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
Upvotes: 0
Reputation: 4447
term> /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep '.app' | grep '.path'
Upvotes: 2
Reputation: 1288
Why are you passing both a NSURL
parameter and an NSString
one?
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]);
Upvotes: 4