2

How can I get installed apps in mac os x programmatically either through C code or Objective-C code?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pavanmvn
  • 749
  • 10
  • 21

1 Answers1

5

It is possible to get all the app files using the spotlight API. Specifically, NSMetadataQuery class..

-(void)doAQuery {
    query = [[NSMetadataQuery alloc] init];
   // [query setSearchScopes: @[@"/Applications"]];  // If you want to find applications only in /Applications folder

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemKind == 'Application'"];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:nil];
    [query setPredicate:predicate];
    [query startQuery];
}

-(void)queryDidFinishGathering:(NSNotification *)notif {
    int i = 0;
    for(i = 0; i< query.resultCount; i++ ){
        NSLog(@"%@", [[query resultAtIndex:i] valueForAttribute:kMDItemDisplayName]);
    }
}

You have various other attributes, such as kMDItemFSName. More attributes can be found here

The terminal version of the above is:

mdfind 'kMDItemKind=Application'
Shanti K
  • 2,873
  • 1
  • 16
  • 31