Cocoa 应用程序的可执行文件可以窃取调用者的焦点吗?

发布于 2024-10-17 21:53:45 字数 432 浏览 4 评论 0原文

假设我有一个标准的 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 技术交流群。

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

发布评论

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

评论(5

花心好男孩 2024-10-24 21:53:45

您的应用程序可以通过调用“窃取焦点”。

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

请参阅 NSApplication 文档以获取更多信息。

Your app can "steal focus" by calling

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

See the NSApplication docs for more info.

茶花眉 2024-10-24 21:53:45

您的意思是输入:

open -a /Applications/Foo.app/

注意 -a 选项

did you mean to type:

open -a /Applications/Foo.app/

note the -a option

但可醉心 2024-10-24 21:53:45

如果您询问如何为您的应用程序(您正在编写的)提供此行为 - 在 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].

恍梦境° 2024-10-24 21:53:45

也许是AppleScript?

tell application "TextEdit"
    activate
end tell

perhaps AppleScript?

tell application "TextEdit"
    activate
end tell
梦里的微风 2024-10-24 21:53:45

您可以通过(至少)三种不同的方式从终端打开应用程序(并与之交互)。

  1. 您可以通过键入 open -a ApplicationName 从终端启动任何应用程序(在 LaunchServices 中注册)(请注意,您不需要结尾的“.app”或提供任何路径)< /p>

  2. < p>您可以通过输入 open /path/to/ApplicationName.app 打开一个应用程序并给出其特定路径(您很少需要它,因为应用程序可能已经在 LaunchServices 中注册)

  3. 您如果您输入 open /path/to/ApplicationName.app/Contents/MacOS/ApplicationName,则可以打开可执行文件并与之交互。这里的区别在于,对于某些应用程序,您可以传递参数或随后在命令行上与它们交互。例如,尝试open /Applications/Mail.app/Contents/MacOS/Mail - 它会给您一个调试日志作为回报。

  4. 即使不使用“open”,您也可以通过直接调用某些应用程序的可执行文件来与它们交互;例如,尝试 /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.

  1. 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)

  2. 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)

  3. 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 try open /Applications/Mail.app/Contents/MacOS/Mail for example - it will give you a debugging log in return.

  4. you can interact with some applications even without using "open" by directly calling their executable; try /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.

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