Cocoa 按钮在应用程序外部启动

发布于 2024-11-06 19:13:34 字数 1023 浏览 0 评论 0原文

我有以下设置:

NSWindow 中的 4x4(总共 16 个)按钮(标准 NSButton 按钮)网格。

当我按下热键组合 (DDHotKey) 时,NSWindow 将出现在前面

现在,我想做的是为我的按钮提供以下功能:

  • 单击按钮时,打开一个显示 /Applications 的对话框/ 目录,并允许我选择其中列出的任何应用程序。

  • 当选择应用程序时,将其存储在变量(我猜)(或字符串?)中,并使其在按下按钮“等效键”时启动该应用程序

我正在环顾四周,但我不确定要做什么或真正去哪里开始寻找……有什么线索吗?

我的 appdelegate.m 文件中有此内容:

- (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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-11-13 19:13:34

您可以使用 NSOpenPanel 来选择应用程序。

然后要启动应用程序,请查看 这个堆栈溢出问题。

You can use an NSOpenPanel to choose the application.

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

呆头 2024-11-13 19:13:34

存储应用程序的路径,然后当您要打开它们时。您可以使用system()函数。

system("open -a /Applications/someApplication.app");

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

system("open -a /Applications/someApplication.app");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文