Cocoa 应用程序的可执行文件可以窃取调用者的焦点吗?
假设我有一个标准的 Cocoa 应用程序调用 Foo.app(就像您通过在 Xcode 中选择“新建项目”>“Cocoa 应用程序”获得的应用程序一样),如果我使用以下命令通过终端打开该应用程序:
open Foo.app/
然后我会在状态栏上看到 Foo 的名称顶部及其窗口处于焦点位置,位于所有其他应用程序的前面。
相反,如果我直接从终端调用埋藏在 .app 文件夹中的可执行文件,例如:
Foo.app/Contents/MacOS/Foo
什么都不会发生。经检查,应用程序确实已打开,但它没有处于焦点状态(终端仍然处于焦点状态),我必须在扩展坞上找到它或找到它的窗口。
Foo 应用程序有什么方法可以确保其在运行时处于焦点状态吗?即使它通过如上所述的可执行文件运行?
Say I have a standard Cocoa Application call Foo.app (like the one you get just by choosing New Project > Cocoa Application in Xcode), if I open the app via a terminal using:
open Foo.app/
Then I see Foo's name on the status bar up top and its window is in focus, in front of all other apps.
If instead I directly call from the terminal the executable buried in the .app folder, like:
Foo.app/Contents/MacOS/Foo
Nothing appears to happen. On inspection the app has indeed opened but it is not in focus (the terminal still is), I have to find it on the dock or find its window.
Is there any way for the Foo application to make sure its in focus when its run? Even if its run via its executable as described above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的应用程序可以通过调用“窃取焦点”。
请参阅 NSApplication 文档以获取更多信息。
Your app can "steal focus" by calling
See the NSApplication docs for more info.
您的意思是输入:
注意
-a
选项did you mean to type:
note the
-a
option如果您询问如何为您的应用程序(您正在编写的)提供此行为 - 在
applicationWillFinishLaunching:
中,您可以调用[NSApp activateIgnoringOtherApps:YES]
。If you're asking how to give your app (that you're writing) this behavior -- within
applicationWillFinishLaunching:
you could call[NSApp activateIgnoringOtherApps:YES]
.也许是AppleScript?
perhaps AppleScript?
您可以通过(至少)三种不同的方式从终端打开应用程序(并与之交互)。
您可以通过键入
open -a ApplicationName
从终端启动任何应用程序(在 LaunchServices 中注册)(请注意,您不需要结尾的“.app”或提供任何路径)< /p>open /path/to/ApplicationName.app
打开一个应用程序并给出其特定路径(您很少需要它,因为应用程序可能已经在 LaunchServices 中注册)您如果您输入
open /path/to/ApplicationName.app/Contents/MacOS/ApplicationName
,则可以打开可执行文件并与之交互。这里的区别在于,对于某些应用程序,您可以传递参数或随后在命令行上与它们交互。例如,尝试open /Applications/Mail.app/Contents/MacOS/Mail
- 它会给您一个调试日志作为回报。/Applications/Firefox.app/Contents/MacOS/firefox-bin —help
。因此,如果您确实想确保命令行启动的应用程序处于焦点位置,请使用方法 1 或 2。
There are (at least) three different ways you open (and interact with) Applications from the Terminal.
you can launch any Application (that is registered in LaunchServices) from the Terminal by typing
open -a ApplicationName
(note that you do not need the trailing ".app" or give any path)you can open an application giving its specific path by typing
open /path/to/ApplicationName.app
(you will rarely need that, given that the applications is likely already registered in LaunchServices)you can open and interact with the executable if you type
open /path/to/ApplicationName.app/Contents/MacOS/ApplicationName
. The difference here is that for some Applications, you can pass arguments or interact with them afterwards on your command line. Go ahead and tryopen /Applications/Mail.app/Contents/MacOS/Mail
for example - it will give you a debugging log in return./Applications/Firefox.app/Contents/MacOS/firefox-bin —help
for example.So if you do want to make sure the command-line-launched application is in focus, use either method 1 or 2.