Reputation: 2847
could you tell me please - which object and which method can i use to fetch information about installed applications on OSX with objective c or macruby?
Upvotes: 5
Views: 5629
Reputation: 1466
[[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
Upvotes: 0
Reputation: 584
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.
Upvotes: 1
Reputation: 209
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
Upvotes: 2
Reputation: 34185
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,
Upvotes: 4
Reputation: 212929
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
Upvotes: 4
Reputation: 4251
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)
Upvotes: -2
Reputation: 49376
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).
Upvotes: 3