4
votes

I've tried setting "Application is Agent" to 1 but the status bar item disappears. When set to 0 both the status bar item and dock icon are showing. How do I show the status bar item but hide the dock icon?

I've tried the following in both awakeFromNib() and applicationDidFinishLaunching() in AppDelegate.swift:

//class scope    
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)

//function scope    
self.statusItem.image = NSImage(named: "myImage")
let menu = NSMenu(title: "MyApp-Menu")
let menuItem = NSMenuItem(title: "title", action: nil, keyEquivalent: "")
menu.addItem(menuItem)
self.statusItem.menu = menu
2

2 Answers

2
votes

Updated for Mac OS 10.14

in AppDelegate:

let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)

func applicationDidFinishLaunching(_ aNotification: Notification) {
    if let button = statusItem.button {
        button.image = NSImage(named:NSImage.Name("StatusBarButtonImage"))
        button.action = #selector(launchFromTray)
    }
    constructMenu()
}

func constructMenu() {
    let menu = NSMenu()
    menu.addItem(NSMenuItem.separator())
    menu.addItem(NSMenuItem(title: "Show", action: #selector(launchFromTray), keyEquivalent: "w"))
    menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))

    statusItem.menu = menu
}

And you should set the Application is agent (UIElement) to YES now. Should work fine, hope this will help someone

0
votes

You want hide the dock icon, you must set "Application is Agent" to "YES" or you can set app to UIElement Application:

 ProcessSerialNumber psn = { 0, kCurrentProcess };
 OSStatus rt = TransformProcessType(&psn, kProcessTransformToUIElementApplication);

According to the parts of codes you're offered I think that you say "the status bar item disappears" is not really disappear, it is just the menu disappears. The status bar can't disappear unless you write the wrong code, but your codes look fine.

If what I think is right, the menu disappears because the app is an agent app. What you need to do is just make it be a UI Element app again:

ProcessSerialNumber psn = { 0, kCurrentProcess };
    OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);

You can't show a menu bar when you run an agent app.Only one can be selected between the agent app and the menu bar.

Agents include background-only applications, faceless background-only applications, and UI elements, but is not a full blown application with a menu bar