我正在创建一个应用程序,它收集 OS X 中已安装应用程序的信息。
我试图找到安装在应用程序文件夹中的所有应用程序,扩展名为
".app"
我创建了一个函数,可以获取已安装应用程序的一些信息,但我正在寻找更多数据,如版本、包 ID 和其他有用信息。
这是我获取属性的方法:
- (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;
}
为什么要传递一个
NSURL
参数和一个NSString
参数?您可以从应用程序的
NSBundle
中获取您正在寻找的信息:
NSBundle *myBundle = [NSBundle bundleWithPath:@"/Applications/SomeApp.app"];
NSLog(@"%@", [myBundle infoDictionary]);
term> /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep '.app' | grep '.path'
可以用bash脚本完成:
#!/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
例子:
安卓工作室:3.6
应用商店:3.0
AppCleaner:3.5
在命令行上,以下命令从 plist 文件中提取指定键的值,而无需
grep
或 tr
:
plutil -extract CFBundleShortVersionString raw "/Applications/$file/Contents/Info.plist"