检查是否存在Mac OS X应用程序

时间:2011-03-01 14:03:37

标签: cocoa macos applescript

我记得有一个Cocoa框架或AppleScript字典来检查计算机上是否安装了具有特定名称的Application包。

我该怎么做? Cocoa,AppleScript或命令行对我都很有用。

3 个答案:

答案 0 :(得分:21)

您应该使用Launch Services来执行此操作,尤其是函数LSFindApplicationForInfo()

你这样使用它:

#import <ApplicationServices/ApplicationServices.h>

CFURLRef appURL = NULL;
OSStatus result = LSFindApplicationForInfo (
                                   kLSUnknownCreator,         //creator codes are dead, so we don't care about it
                                   CFSTR("com.apple.Safari"), //you can use the bundle ID here
                                   NULL,                      //or the name of the app here (CFSTR("Safari.app"))
                                   NULL,                      //this is used if you want an FSRef rather than a CFURLRef
                                   &appURL
                                   );
switch(result)
{
    case noErr:
        NSLog(@"the app's URL is: %@",appURL);
        break;
    case kLSApplicationNotFoundErr:
        NSLog(@"app not found");
        break;
    default:
        NSLog(@"an error occurred: %d",result);
        break;          
}

//the CFURLRef returned from the function is retained as per the docs so we must release it
if(appURL)
    CFRelease(appURL);

答案 1 :(得分:3)

从命令行开始,这似乎就是这样做的:

> mdfind 'kMDItemContentType == "com.apple.application-bundle" && kMDItemFSName = "Google Chrome.app"'

答案 2 :(得分:1)

您也可以使用lsregister

on doesAppExist(appName)
    if (do shell script "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump | grep com.apple.Safari") ¬
    contains "com.apple.Safari" then return true
end appExists

这非常快,你可以很容易地从Python等其他语言中做到这一点。你可能想要使用你的grep来提高它的效率。