如何关闭NSWindow并从停靠菜单中删除应用程序图标

时间:2023-01-21 19:39:01

If you are using DropBox on MacBook you must have seen that once we finish with registration process. It closes the main window application and removes it self from dock item yet the application is running from status menu.

如果你在MacBook上使用DropBox,你必须看到我们完成注册过程。它会关闭主窗口应用程序并从dock项目中自行删除它,但应用程序正在从状态菜单运行。


Visual:

如何关闭NSWindow并从停靠菜单中删除应用程序图标


What I have done so far :

  • My application runs perfectly with show NSWindow
  • 我的应用程序与show NSWindow完美运行
  • When I login from my front application window I am able to create a status menu icon with popover
  • 当我从前面的应用程序窗口登录时,我可以使用popover创建一个状态菜单图标
  • In this step I want to remove the NSWindow and Dock Icon yet the application should be accessible from status menu.
  • 在这一步中,我想删除NSWindow和Dock Icon,但应该可以从状态菜单访问该应用程序。

How to achieve this?

怎么做到这一点?

FYI : I am not expecting any code, just understanding or a quick hint. I will figure out the rest :)

仅供参考:我不期待任何代码,只是理解或快速提示。我会弄明白其余的:)


Resource I am referring to,

Later I will configure my app for launchd. So at system startup it can launch and shows it self in status menu. Now I am I looking at a good resource(Of course its good because its Apple Doc) but as I am a naive programmer, does anyone have a good example link? which shows how to add application in launched?

稍后我会为launchd配置我的应用程序。因此,在系统启动时,它可以启动并在状态菜单中自动显示。现在我正在寻找一个好的资源(当然它很好,因为它的Apple Doc)但是因为我是一个天真的程序员,有没有人有一个很好的示例链接?其中显示了如何在启动时添加应用程序?

EDIT:

I will be following this tutorial to add into startup, later user can change their preferences if they want, They can choose turn on my app or turn off on startup

我将按照本教程添加到启动中,以后用户可以根据需要更改自己的首选项,他们可以选择启用我的应用程序或启动时关闭

3 个解决方案

#1


2  

As Teak mentioned you want the "Application is agent (UIElement)" key in your info.plist. This key means you won't have a dock icon... which is what you want. However there is no need to use the "Application is background only" key. This key prevents you from putting any windows on the screen, which is unnecessary. So I would leave this key out.

正如Teak所说,您希望info.plist中的“Application is agent(UIElement)”键。这个键意味着你没有停靠图标......这就是你想要的。但是,不需要使用“Application is background only”键。此键可以防止您在屏幕上放置任何窗口,这是不必要的。所以我会留下这把钥匙。

In general setup your app so no window automatically is shown at startup. Do some sort of check at launch to see if configuration is needed. If it is then make your configuration window visible. You can even have a "configuration..." menu item in your status menu so your user can pop open the config window any time needed. It's really very simple, but just don't use the "Application is background only" info.plist key.

通常设置您的应用程序,以便在启动时不会自动显示窗口。在启动时进行某种检查以查看是否需要配置。如果是,则使配置窗口可见。您甚至可以在状态菜单中有一个“配置...”菜单项,这样您的用户就可以随时弹出配置窗口。它真的很简单,但只是不要使用“Application is background only”info.plist键。

By the way, it's a little difficult to make a window come to the front. The trick is to make your application come frontmost first like this...

顺便说一下,让窗户走到前面有点困难。诀窍是让你的应用程序首先像这样......

[NSApp activateIgnoringOtherApps:YES];
[someWindow makeKeyAndOrderFront:nil];

#2


4  

To run your app as a status menu only, you add one of the following keys to your info.plist file:

要仅将您的应用作为状态菜单运行,请将以下键之一添加到info.plist文件中:

Application is agent (UIElement)

申请是代理人(UIElement)

or:

要么:

Application is background only

申请仅限后台

As for switching between the two, Im not sure the best way, but I would suggest making the setup process its own app. (An app with an icon and window) You would than contain the app inside your menu item app, and automatically launch it when the user launches the program...

至于在两者之间切换,我不确定最好的方法,但我建议将设置过程作为自己的应用程序。 (带有图标和窗口的应用程序)您可以在菜单项应用程序中包含应用程序,并在用户启动程序时自动启动它...

#3


0  

Usually the situation mentioned in the dropbox sample solved using two separate application, one for the menu and one for the other ui part (that has the mentioned dock icon f.e.) and the two process cooperate via f.e. ipc.

通常,Dropbox示例中提到的情况使用两个单独的应用程序解决,一个用于菜单,另一个用于另一个ui部分(具有提到的停靠图标f.e.),并且两个过程通过f.e.合作。 IPC。

doing the same in one app is a bit tricky you can find my detail about that here

在一个应用程序中做同样的事情有点棘手,你可以在这里找到我的细节

Cocoa - go to foreground/background programmatically

Cocoa - 以编程方式转到前台/后台

#1


2  

As Teak mentioned you want the "Application is agent (UIElement)" key in your info.plist. This key means you won't have a dock icon... which is what you want. However there is no need to use the "Application is background only" key. This key prevents you from putting any windows on the screen, which is unnecessary. So I would leave this key out.

正如Teak所说,您希望info.plist中的“Application is agent(UIElement)”键。这个键意味着你没有停靠图标......这就是你想要的。但是,不需要使用“Application is background only”键。此键可以防止您在屏幕上放置任何窗口,这是不必要的。所以我会留下这把钥匙。

In general setup your app so no window automatically is shown at startup. Do some sort of check at launch to see if configuration is needed. If it is then make your configuration window visible. You can even have a "configuration..." menu item in your status menu so your user can pop open the config window any time needed. It's really very simple, but just don't use the "Application is background only" info.plist key.

通常设置您的应用程序,以便在启动时不会自动显示窗口。在启动时进行某种检查以查看是否需要配置。如果是,则使配置窗口可见。您甚至可以在状态菜单中有一个“配置...”菜单项,这样您的用户就可以随时弹出配置窗口。它真的很简单,但只是不要使用“Application is background only”info.plist键。

By the way, it's a little difficult to make a window come to the front. The trick is to make your application come frontmost first like this...

顺便说一下,让窗户走到前面有点困难。诀窍是让你的应用程序首先像这样......

[NSApp activateIgnoringOtherApps:YES];
[someWindow makeKeyAndOrderFront:nil];

#2


4  

To run your app as a status menu only, you add one of the following keys to your info.plist file:

要仅将您的应用作为状态菜单运行,请将以下键之一添加到info.plist文件中:

Application is agent (UIElement)

申请是代理人(UIElement)

or:

要么:

Application is background only

申请仅限后台

As for switching between the two, Im not sure the best way, but I would suggest making the setup process its own app. (An app with an icon and window) You would than contain the app inside your menu item app, and automatically launch it when the user launches the program...

至于在两者之间切换,我不确定最好的方法,但我建议将设置过程作为自己的应用程序。 (带有图标和窗口的应用程序)您可以在菜单项应用程序中包含应用程序,并在用户启动程序时自动启动它...

#3


0  

Usually the situation mentioned in the dropbox sample solved using two separate application, one for the menu and one for the other ui part (that has the mentioned dock icon f.e.) and the two process cooperate via f.e. ipc.

通常,Dropbox示例中提到的情况使用两个单独的应用程序解决,一个用于菜单,另一个用于另一个ui部分(具有提到的停靠图标f.e.),并且两个过程通过f.e.合作。 IPC。

doing the same in one app is a bit tricky you can find my detail about that here

在一个应用程序中做同样的事情有点棘手,你可以在这里找到我的细节

Cocoa - go to foreground/background programmatically

Cocoa - 以编程方式转到前台/后台