在 OS X 中查找已安装应用程序的版本

问题描述 投票:0回答:4

我正在创建一个应用程序,它收集 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
4个回答
4
投票
  1. 为什么要传递一个

    NSURL
    参数和一个
    NSString
    参数?

  2. 您可以从应用程序的

    NSBundle
    中获取您正在寻找的信息:

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

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

0
投票

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


0
投票

在命令行上,以下命令从 plist 文件中提取指定键的值,而无需

grep
tr

plutil -extract CFBundleShortVersionString raw "/Applications/$file/Contents/Info.plist"
© www.soinside.com 2019 - 2024. All rights reserved.