简体   繁体   中英

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.

In fact, there's the possibility that the app runs twice at the same time - once under the current user and once as root.

I like to inform the user when the app is running under 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. 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.

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

…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; (2) the name of the executable inside the package, (3) the long name used in many places for display purposes only; (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; and (5) the process name of a running application. They aren't always the same, especially in Microsoft and Adobe products.

Bill Cheeseman

From what I can tell, .

.

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

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. 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. You will get two icons bouncing at launch time, though.

OS X apps do not normally run with multiple instances. Look at any app developed by Apple for example. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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