Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application selection - Cocoa

I want to display a dialog which allows the user to browse through their current applications (/Applications) and I need to get the full path of that application (selected). How would I go about this? Code samples are appreciated.

Thanks.

like image 677
Sam Avatar asked Apr 14 '26 15:04

Sam


1 Answers

Something like this would be a good start:

NSArray *appsDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, 
                       NSLocalDomainMask,  YES);
NSString *appsDir = nil;
if ([appsDirs count]) appsDir = [appsDirs objectAtIndex:0];

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setTitle:NSLocalizedString(@"Add Application", @"")];
[openPanel setMessage:NSLocalizedString(@"Choose the application to add.", @"")];
[openPanel setPrompt:NSLocalizedString(@"Add Application", @"")];
[openPanel setAllowsMultipleSelection:YES]; // ?
[openPanel setCanChooseDirectories:NO];
[openPanel setDelegate:self];

NSInteger result = [openPanel runModalForDirectory:appsDir
                                              file:nil
                                             types:
                    [NSArray arrayWithObject:NSFileTypeForHFSTypeCode('APPL')]];

if (result == NSOKButton) {
    NSArray *fileURLs = [openPanel URLs];
    for (NSURL *URL in fileURLs) {
         NSString *path = [URL path];
         // add path, etc.
    }
}
like image 164
NSGod Avatar answered Apr 16 '26 04:04

NSGod