来自命令行的 Cocoa 应用程序

发布于 2024-12-15 18:57:33 字数 721 浏览 3 评论 0原文

我一直在尝试按照说明创建一个不使用 NIB 或 XIB 的 Cocoa(桌面)应用程序 这里。一切都很好,从 Terminal.app 我可以成功运行该应用程序。然而,它的生成没有任何常见的装饰,例如菜单栏或停靠图标,位于终端窗口后面,并且看起来与终端进程有很大的联系。

我怀疑这可能是直接从命令行运行二进制文件本身的结果,但玩了一下我发现至少 Calculator.app 非常乐意生成一个“真正的”应用程序调用时使用:

$ /Applications/Calculator.app/Contents/MacOS/Calculator

我在这里缺少什么(如果有的话)?我还尝试加载 XIB 文件,通过

NSNib *mainNib = [[NSNib alloc] initWithContentsOfURL:[[NSURL URLWithString:@"../MainMenu.xib"] absoluteURL]];
[NSNib instantiateNibWithOwner:application topLevelObjects:nil];

它似乎可以找到 XIB,但没有什么区别(即 GUI 未完全加载)。

I've been attempting to create a Cocoa (desktop) application without using NIBs or XIBs, following the instructions here. All is well, and from Terminal.app I can successfully run the application. However it's spawned without any of the usual decorations, such as a menu bar or a dock icon, behind the terminal window and seemingly attached very much to the terminal process.

I suspected that this might be a consequence of running the binary itself directly from the command line, but playing around a bit I found that at least Calculator.app is more than happy to spawn a "real" application when called using:

$ /Applications/Calculator.app/Contents/MacOS/Calculator

What am I missing, if anything, here? I have also tried loading a XIB file via

NSNib *mainNib = [[NSNib alloc] initWithContentsOfURL:[[NSURL URLWithString:@"../MainMenu.xib"] absoluteURL]];
[NSNib instantiateNibWithOwner:application topLevelObjects:nil];

which seems to find the XIB, but makes no difference (i.e. the GUI isn't fully loaded).

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

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

发布评论

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

评论(2

不气馁 2024-12-22 18:57:33

经过一番摆弄,我找到了问题的解决方案。通过 TransformProcessType 转换进程类型,然后使用准确命名的 [NSApp activateIgnoringOtherApps:YES] 就成功了:(

ProcessSerialNumber psn = { 0, kCurrentProcess }; 
OSStatus returnCode = TransformProcessType(& psn, kProcessTransformToForegroundApplication);

[NSApp activateIgnoringOtherApps:YES];

我还添加了一个自定义菜单来完成它)。感谢 Mankarse 的指点!

After a bit of fiddling, I found the solution to the issue. Transforming the process type via TransformProcessType and then using the accurately-named [NSApp activateIgnoringOtherApps:YES] did the trick:

ProcessSerialNumber psn = { 0, kCurrentProcess }; 
OSStatus returnCode = TransformProcessType(& psn, kProcessTransformToForegroundApplication);

[NSApp activateIgnoringOtherApps:YES];

(I also added a custom menu to finish it off). Thanks to Mankarse for the pointers!

半窗疏影 2024-12-22 18:57:33

你需要一个 NSApplication.. 你为什么要这样做?

如果您创建一个新的 GUI 应用程序,请查看 main.m,您会发现它在那里生成了一个 NSApplication。

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc, (const char **)argv);
}

顺便说一句,这要求您有一个捆绑包,如果您想在没有终端的情况下启动命令行实用程序,您可以使用 launchd 或创建一个捆绑包,只需在查找器中双击即可。

You need a NSApplication.. why are you even doing this?

If you create a new GUI application, look at main.m you see that it is spawninng a NSApplication there.

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc, (const char **)argv);
}

This requires you to have a bundle btw and if you want to launch a command line utility without the terminal you could either use launchd or create a bundle that you simply double click in the finder.

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