来自命令行的 Cocoa 应用程序
我一直在尝试按照说明创建一个不使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一番摆弄,我找到了问题的解决方案。通过
TransformProcessType
转换进程类型,然后使用准确命名的[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:(I also added a custom menu to finish it off). Thanks to Mankarse for the pointers!
你需要一个 NSApplication.. 你为什么要这样做?
如果您创建一个新的 GUI 应用程序,请查看 main.m,您会发现它在那里生成了一个 NSApplication。
顺便说一句,这要求您有一个捆绑包,如果您想在没有终端的情况下启动命令行实用程序,您可以使用 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.
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.