我应该确切地传递给-[NSApp activateIgnoringOtherApps:]以使我的应用程序自然启动并“启动".与大多数其他OS X应用程序相比? [英] What exactly should I pass to -[NSApp activateIgnoringOtherApps:] to get my application to start "naturally" in comparison to most other OS X apps?

查看:95
本文介绍了我应该确切地传递给-[NSApp activateIgnoringOtherApps:]以使我的应用程序自然启动并“启动".与大多数其他OS X应用程序相比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学会了如何自行启动NSApplications时,我使用的代码(基于

When I learned how to start NSApplications on my own, the code I used (based on here and here) did

[NSApp activateIgnoringOtherApps:YES];

在启动时强制应用程序在前面.

which forces the app to the front at startup.

我想知道大多数其他应用程序的功能.我希望能够直接从二进制文件和应用程序捆绑包中运行程序,并且我没有使用Xcode来构建它(原始构建).所以,我宁愿这种行为自然发生,可以这么说.

I'd like to know what most other apps do. I want to be able to run programs both directly from the binary and from an app bundle, and I'm not using Xcode to build this (raw building). So I'd rather this act naturally, so to speak.

文档确实说Finder问题 NO ,但是...为什么是Finder?这不是从流程内部而不是外部运行的方法吗?(我无法控制选择.)Dock和其他可能的入口点呢?

The docs do say Finder issues NO, but... why Finder? Isn't this a method that's run from within the process, not outside? (I'm not in control of the choice.) And what about the Dock and other possible entry points?

我什至反汇编了 10.8 的 NSApplicationMain() 来看看它做了什么,但据我所知,32 位版本,除非这个轻量级启动"事情产生了这个选择器,这个选择器永远不会被调用.

I even went so far as to disassemble 10.8's NSApplicationMain() to see what it did, but as far as I can tell from the 32-bit version, unless this "light launch" thing issues this selector, this selector is never called.

这个问题有答案吗?谢谢...如果这令人困惑,抱歉;我试图用尽可能清晰的措词.

Is there an answer to this question? Thanks... and sorry if this is confusing; I tried to word it as clearly as possible.

推荐答案

应用程序通常根本不会调用 -activateIgnoringOtherApps:.而且,一般而言,不应该.当然,它不会出现在 NSApplicationMain()中,这还为时过早,与实际的应用启动关系还很遥远.

Apps normally do not call -activateIgnoringOtherApps: at all. And, generally speaking, shouldn't. Certainly, it wouldn't be in NSApplicationMain(), which is too early and fairly distantly related to actual app start-up.

应用通常由启动服务(Finder,Dock和/usr/bin/open 以及可能打开您的应用或您处理的文件).大致上会发生这样的情况,启动服务会停用调用它的应用程序以打开其他内容,然后在启动的应用程序中,可可的内部部件执行类似(但不一定与)相同的操作: [NSApp activateIgnoringOtherApps:NO] .这样,只有在这两个事件之间的时间间隔内未激活任何其他功能时,启动的应用程序才会激活.如果该间隔很长(因为有些东西很慢),并且用户同时又切换到其他东西,则您不想从他们切换到的任何东西上抢走焦点.

Apps are normally launched by Launch Services (which is what is used by the Finder, the Dock, and /usr/bin/open, as well as any other app that might open yours or a document which yours handles). Roughly what happens is that Launch Services deactivates the app which called it to open something else and then, in the launched app, Cocoa's internals do something like (but not necessarily identical to) [NSApp activateIgnoringOtherApps:NO]. In this way, the launched app only activates if nothing else was activated in the interval between those two events. If that interval is long (because something was slow) and the user switched to something else in the meantime, you don't want to steal focus from whatever they switched to.

您仅应在用户未激活Launch Services自动取消激活当前上下文的情况下,调用 [NSApp activateIgnoringOtherApps:YES] 来响应用户激活您的应用程序的请求.例如,如果您有一个命令行程序将其自身转换为GUI应用程序(使用-[NSApplication setActivationPolicy:] 或已弃用的 TransformProcessType()),则用户运行该工具意味着他们希望它处于活动状态.但是Terminal是活动的,不会仅仅由于运行程序而自发地停用.因此,程序具有可以窃取焦点.

You should only call [NSApp activateIgnoringOtherApps:YES] in response to a user request to activate your app in a context which won't include the automatic deactivation of the current app by Launch Services. For example, if you have a command-line program which transforms itself into a GUI app (using -[NSApplication setActivationPolicy:] or the deprecated TransformProcessType()), then the user running that tool means they want it active. But Terminal is active and won't be deactivated spontaneously just by virtue of having run your program. So, the program has to steal focus.

如果您的程序是捆绑的应用程序,则应使用/usr/bin/open 从命令行运行它,而不是直接在捆绑软件中执行可执行文件.然后,您根本不需要调用 -activateIgnoringOtherApps:,传递什么值的问题就没有意义了.

If your program is a bundled app, then running it from the command line should be done with /usr/bin/open rather than directly executing the executable inside the bundle. Then, you don't need to call -activateIgnoringOtherApps: at all and the question of what value to pass is moot.

这篇关于我应该确切地传递给-[NSApp activateIgnoringOtherApps:]以使我的应用程序自然启动并“启动".与大多数其他OS X应用程序相比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆