9

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.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184

4 Answers4

21

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);
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
3

From the command line this seems to do it:

> mdfind 'kMDItemContentType == "com.apple.application-bundle" && kMDItemFSName = "Google Chrome.app"'
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
1

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.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
Clark
  • 833
  • 4
  • 6
  • You're right that it's a solution for languages that can't use native APIs. However, it would be overkill to call a command-line tool from a Cocoa app, since it just queries the very same Launch Services API. – Rob Keniger Mar 02 '11 at 01:11
  • True but the OP wasn't clear how he was using it. Plus others will certainly find this page for some other similar problem. – Clark Mar 02 '11 at 02:39
0

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
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8