Steve McLeod
Steve McLeod

Reputation: 52448

Check if a Mac OS X application is present

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.

How do I do this? Either Cocoa, AppleScript, or command line are useful to me.

Upvotes: 9

Views: 5083

Answers (4)

Robert Kniazidis
Robert Kniazidis

Reputation: 1878

Quick and easy, like this:

appInstalled("com.apple.Safari") --> true
appInstalled("com.api.finder") --> false
appInstalled("com.apple.finder") --> true
appInstalled("org.m0k.transmission") --> true, on my Mac, Transmission.app
appInstalled("org.videolan.vlc") --> true, on my Mac, VLC.app
appInstalled("com.apple.Music") --> true, on my Mac, Music.app
appInstalled("com.apple.iTunes") --> false, on my Mac, iTunes.app

on appInstalled(bundleID)
    try
        application id bundleID
        return true
    end try
    return false
end appInstalled

Upvotes: 0

Rob Keniger
Rob Keniger

Reputation: 46020

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);

Upvotes: 21

Clark
Clark

Reputation: 818

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.

Upvotes: 1

Steve McLeod
Steve McLeod

Reputation: 52448

From the command line this seems to do it:

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

Upvotes: 3

Related Questions