简体   繁体   English

OS X:在运行时设置Dock和菜单栏中显示的应用程序名称

[英]OS X: Setting at runtime the Application name as it appears in Dock and Menu Bar

I have an app that may get launched with current user's or with root permissions. 我有一个可以使用当前用户或root权限启动的应用程序。

In fact, there's the possibility that the app runs twice at the same time - once under the current user and once as root. 事实上,应用程序可能同时运行两次 - 一次在当前用户下,一次在root下。

I like to inform the user when the app is running under root. 我想在应用程序在root下运行时通知用户。 Currently, I show this in its window title, but I'd prefer to change the app's name so that it reads "Appname (Root)" where appropriate. 目前,我在其窗口标题中显示了这一点,但我更愿意更改应用程序的名称,以便在适当的位置显示“Appname(Root)”。 That way, the name would appear both in the menu bar and in the Dock with this "root" info, and it would allow the user to tell which of the two app instances in the Dock is the root version. 这样,名称将出现在菜单栏和Dock中,带有这个“根”信息,它将允许用户告诉Dock中的两个应用程序实例中的哪一个是根版本。

Does someone know if the app name can be changed by code, ie by the app itself, or by its launcher? 有人知道应用程序名称是否可以通过代码更改,即应用程序本身或其启动程序?

If not, my only option appears to be adding a badge to the Dock icon that says "root", but that's my last resort, as it won't work for the menu bar nor for other tools that display the app names (ie other app launchers such as DragThing). 如果没有,我唯一的选择似乎是在Dock图标上添加一个徽章,上面写着“root”,但这是我的最后一招,因为它不适用于菜单栏,也不适用于显示应用名称的其他工具(即其他应用程序启动器,如DragThing)。

…there are at least five application names floating around, at least in concept: (1) the file name the Finder sees, which in the case of an application package is the package (bundle) name; ...至少在概念上至少有五个应用程序名称:(1)Finder看到的文件名,在应用程序包的情况下是包(bundle)名称; (2) the name of the executable inside the package, (3) the long name used in many places for display purposes only; (2)包内可执行文件的名称,(3)在很多地方使用的长名称仅用于显示目的; (4) the short name used as the application menu title and in a few other places where a long name won't fit for display purposes; (4)用作申请菜单​​标题的短名称,以及长名称不适合显示目的的其他一些地方; and (5) the process name of a running application. (5)正在运行的应用程序的进程名称。 They aren't always the same, especially in Microsoft and Adobe products. 它们并不总是相同,特别是在Microsoft和Adobe产品中。

Bill Cheeseman - 比尔芝士

From what I can tell, the name in the dock is the name of the application bundle (sans ".app") on the filesystem . 据我所知, dock中的名称是文件系统上的应用程序包名称(无“.app”)

The value under the CFBundleName key in info.plist is what shows up in the menubar . info.plist中CFBundleName键下的值是菜单栏中显示的值

As far as I know, changing either these at runtime isn't going to work… but what you can do is have two versions of your app, a root and non-root version, inside your user-facing .app bundle. 据我所知,在运行时更改这些内容并不起作用......但是你可以做的是在面向用户的.app包中有两个版本的应用程序,一个root和非root版本。 When the user-facing app is launched, it checks if it's running as root or not, and launches the appropriate copy of the real app, which has the CFBundleName and file-name you want. 当启动面向用户的应用程序时,它会检查它是否以root用户身份运行,并启动真实应用程序的相应副本,该应用程序具有您想要的CFBundleName和文件名。

I wish I knew of a more elegant solution. 我希望我知道更优雅的解决方案。

That label comes out of your Info.plist and I don't believe it's ever consulted again after it's been launched. 该标签来自您的Info.plist,我不相信它在推出后再次被咨询过。 If you want to have a parent application that does nothing but launch the real application (possibly from inside its own bundle) after munging its Info.plist, then that might give you the effect you're hoping for. 如果你想让一个父应用程序在修改它的Info.plist之后什么也不做,只是启动真实应用程序(可能来自它自己的包内),那么这可能会给你你想要的效果。 You will get two icons bouncing at launch time, though. 不过,你会在发布时获得两个图标。

OS X apps do not normally run with multiple instances. OS X应用程序通常不会与多个实例一起运行。 Look at any app developed by Apple for example. 看看Apple开发的任何应用程序。 It would be more common to allow for multiple documents/windows, in this case each window could be either a root or non-root window. 允许多个文档/窗口更常见,在这种情况下,每个窗口可以是根窗口或非根窗口。

The most common way to allow for root authentication however is to have a padlock icon that can be clicked to switch to root mode with a password prompt, this can be seen in many of the System Preferences panels. 但是,允许root身份验证的最常用方法是使用一个挂锁图标,可以单击以通过密码提示切换到root模式,这可以在许多“系统偏好设置”面板中看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM