Check if a Mac OS X application is present

前端 未结 3 1786
轻奢々
轻奢々 2021-02-03 12:29

I recall there being a Cocoa framework or AppleScript dictionary to check if an Application bundle with a specific name is installed at all, anywhere on the computer.

Ho

相关标签:
3条回答
  • 2021-02-03 13:16

    From the command line this seems to do it:

    > mdfind 'kMDItemContentType == "com.apple.application-bundle" && kMDItemFSName = "Google Chrome.app"'
    
    0 讨论(0)
  • 2021-02-03 13:18

    You should use Launch Services to do this, specifically the function LSFindApplicationForInfo().

    You use it like so:

    #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);
    
    0 讨论(0)
  • 2021-02-03 13:29

    You can also use 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
    

    That's pretty fast and you can do it from other languages like Python quite easily. You would want to play around with what you grep to make it most efficient.

    0 讨论(0)
提交回复
热议问题