0

I have the following setup:

A grid of 4x4 (16 total) buttons (standard NSButton buttons) in an NSWindow.

The NSWindow will come to the front when I press a hotkey combination (DDHotKey)

Now, what I'd like to do is give my buttons the following functionality:

  • When the button is clicked, open a dialog that shows the /Applications/ directory and allow me to select any of the applications listed there.

  • When the application is selected store it in a variable (I'm guessing) (or string?) and make it so that when the buttons Key Equivalent is pressed, that application launches

I'm looking around and I'm not exactly sure what to do or really where to begin looking...any clues?

I have this in my appdelegate.m file:

- (void)openDoc:(id)sender
{
    int result;
    NSArray *fileTypes = [NSArray arrayWithObject:@"td"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

[oPanel setAllowsMultipleSelection:YES];
result = [oPanel runModalForDirectory:NSHomeDirectory()
                file:nil types:fileTypes];
if (result == NSOKButton) {
    NSArray *filesToOpen = [oPanel filenames];
    int i, count = [filesToOpen count];
    for (i=0; i<count; i++) {
        NSString *aFile = [filesToOpen objectAtIndex:i];
        id currentDoc = [[ToDoDoc alloc] initWithFile:aFile];
    }
}
}

How do I link the button to it?

Zrb0529
  • 800
  • 9
  • 25

2 Answers2

2

You can use an NSOpenPanel to choose the application.

Then to launch the application, take a look at this stack overflow question.

Community
  • 1
  • 1
edc1591
  • 10,146
  • 6
  • 40
  • 63
  • Ok, let me see if I understand. I'll need to subclass NSButton, right? What I'll need to do is make a button subclass that has a click event that will open the NSOpenPanel and where it's Key Equivalent event will execute the program loaded...right? – Zrb0529 May 13 '11 at 22:12
  • No, take a look at the `NSButton` documentation. You set an IBAction that opens the `NSOpenPanel` and you can set the key equivalent with `setKeyEquivalent:`. – edc1591 May 14 '11 at 00:26
  • You link it from the xib file. If you don't know how to do this, I recommend reading a tutorial on Xcode, or check out Apple's documentation. – edc1591 May 14 '11 at 03:50
  • wow never mind, I'm a loser...I fixed it...thanks for your help – Zrb0529 May 14 '11 at 22:46
1

store the path to application, then when you want to open them. You can use the system() function.

system("open -a /Applications/someApplication.app");
Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • rad. I answered the second half of the question, what is wrong with that? – Grady Player May 13 '11 at 22:45
  • 1
    using `system()` is very dangerous, and there are far superior (and sanctioned) ways of launching an external application. I can think of 3 off the top of my head that don't come anywhere near `system()`. – Dave DeLong May 14 '11 at 06:03
  • Ok, system can be dangerous, but I don't see the boogie man here, it is no more dangerous than using the terminal, I see possible problems with portabilty but not safety. – Grady Player May 14 '11 at 15:03
  • out of curiosity Dave, why is using system() dangerous? I plan on googling as soon as I type this..but a professional answer is always good to have :-) – Zrb0529 May 14 '11 at 23:18