Cocoa应用程序的可执行程序可以从调用者窃取焦点吗? [英] Can a Cocoa app's executable steal focus from caller?

查看:167
本文介绍了Cocoa应用程序的可执行程序可以从调用者窃取焦点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个标准的Cocoa应用程序调用Foo.app(像在Xcode中选择New Project> Cocoa Application)。如果我通过终端打开应用程序:

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/

然后我在状态栏上方看到Foo的名称,它的窗口是焦点,在所有其他应用程序的前面。

Then I see Foo's name on the status bar up top and its window is in focus, in front of all other apps.

如果相反,我直接从终端调用可执行文件埋在.app文件夹中,如:

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.

有任何方式的Foo应用程序,以确保它的焦点在其运行时?即使它通过其可执行文件如上所述运行

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?

推荐答案

您的应用程序可以通过调用

Your app can "steal focus" by calling

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

请参阅 NSApplication docs了解更多信息。

See the NSApplication docs for more info.

这篇关于Cocoa应用程序的可执行程序可以从调用者窃取焦点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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