【问题标题】:Find version of installed application in OS X在 OS X 中查找已安装应用程序的版本
【发布时间】:2014-03-19 14:20:06
【问题描述】:

我正在创建一个应用程序,它收集 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;
}

【问题讨论】:

    标签: macos cocoa nsfilemanager .app


    【解决方案1】:
    1. 为什么要同时传递 NSURL 参数和 NSString 一个?

    2. 您可以从应用程序的NSBundle 获取您要查找的信息:

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

    【讨论】:

    • 使用“CFBundleVersion”键
    • 感谢您的回答,但幸运的是我发现了相同的 ;) 并且还得到了一些有趣的东西!
    【解决方案2】:
    term> /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep '.app' | grep '.path'
    

    【讨论】:

    • 你能详细说明如何获得这个吗,我是 mac os x 的新手
    • 在终端窗口中输入上述命令。很多喷。您可能想要捕获它并使用编辑器进行搜索。
    【解决方案3】:

    可以使用 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
    

    示例:

    Android Studio:3.6
    应用商店:3.0
    AppCleaner:3.5

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      相关资源
      最近更新 更多