swiftmacosprogress-barstatusbarnsstatusbar

MacOS create a ProgressBar on StatusBar


I'm looking for a way to put a ProgressBar onto the Mac status bar as a button. I want to have something like this:

Sample Image

or

enter image description here

What I can do is this:

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

    [...]

    if let button = statusItem.button {
                button.image = NSImage(named:NSImage.Name("JustSomeImage"))
                button.title = "Hello"
                button.action = #selector(togglePopover(_:))

            }

But with NSStatusItem, I'm limited to only use a button and menu object to manipulate my NSStatusBar. So all I can do is putting an image to the bar with some text. I have searched a lot for a solution and read the doc, but I couldn't find a way to put a ProgressBar up there somehow. Anyone has an idea how I could do that?


Solution

  • Since NSButton is a subclass of NSView you can add any view you would like to add. Below you can find a small code snippet in Objective C but it should not be difficult to turn it into Swift.

    NSProgressIndicator *indicator = [[NSProgressIndicator alloc] initWithFrame:button.frame];
    [button addSubview:indicator];
    

    Best wishes and great success.