开发者

How can I discover what apps are installed on OS X using Objective C or Macruby?

could you tell me please - which obje开发者_如何转开发ct and which method can i use to fetch information about installed applications on OSX with objective c or macruby?


You can just call the Apple command-line tool system_profiler, e.g.

% system_profiler SPApplicationsDataType

For C or Objective-C you could use system() to do this.

For more info:

% man system

% man system_profiler


If you meant to list all of GUI apps, you can use Launch Services. For the list of functions, see the reference.

Do not manually list .app bundles! It's already done by the system, and you just have to query it. There is a command-line program called lsregister at

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister

which can be used to get the detailed dump of the Launch Service database. This tool is sometimes helpful, but don't depend on that in a shipping program,


OS X doesn't really have a notion of "installing an app". OS X applications are self contained bundles that appear in the Finder as a single virtual file. There's no central registry, just a convention that applications are placed in /Applications or ~/Applications for a per user "install".

About the best you can do will be enumerate those directories for their contents, and every ".app" directory you find is an application. It will not be a exhaustive (I often run small apps from e.g. my Desktop if I've just downloaded them).


I moved in a following way:

  • execute command system_profiler SPApplicationsDataType -xml from code. Key SPApplicationsDataType means that only application data is required. -xml means that I expect to see xml result for easier parsing.

  • parse result array

Here you can find nice example regarding command execution from code: Execute a terminal command from a Cocoa app

Total code example is looked like following:

NSStirng * commangString = @"system_profiler SPApplicationsDataType -xml"; 
NSData * resultData = [CommandLineTool runCommand:commangString];
NSArray * appArray = [PlistManager objectFromData:resultData];
// parse array of NSDictionaries here ....



// method from PlistManager
+ (id) objectFromData: (NSData *) data {
    NSString *errorString = nil;
    NSPropertyListFormat format;
    id object = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&errorString];
    if (!errorString) {
        return object;
    } else {
        NSLog(@"Error while plist to object conversion: %@", errorString);
        return nil;
    }
}

// runCommand method was used from post I mentioned before


Use Spotlight via NSMetadataQuery.

You can view the results you'll get using mdfind at the command line:

mdfind "kMDItemContentType == 'com.apple.application-bundle'"

Works like a charm and very very fast.


[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.google.Chrome"];

This would return a path to Google Chrome app if it is installed, nil otherwise.

If you don't know the BundleID, there are two options to find it:

1) Open .plist file of the app by right-clicking the app icon and choosing Show Package Contents option.

Default path to Chrome .plist is /Applications/Google\ Chrome.app/Contents/Info.plist

2) Use lsregister alongside with grep.

Try typing the following into the Terminal app, you will find the BundleID there:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep -i chrome


As it is a unix-like OS there is no real way of telling what you have installed (unless of course you have control of the other app - then there is a lot of ways of doing this for obvious reasons).

Generally apps get put into /Applications or ~/Applications BUT you can run it from anywhere and not those folders (just like a unix machine)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜