0

I am trying to create an application that will display a menu bar extra on macOS.

I used a lot of code I've found on the web so far, but, while all of them compile, none actually display the menu.

The code snippets are more or less like this:

@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { var statusBarItem: NSStatusItem! func applicationDidFinishLaunching(_ aNotification: Notification) { let statusBar = NSStatusBar.system statusBarItem = statusBar.statusItem( withLength: NSStatusItem.squareLength) statusBarItem.button?.title = "🌯" let statusBarMenu = NSMenu(title: "Cap Status Bar Menu") statusBarItem.menu = statusBarMenu statusBarMenu.addItem( withTitle: "Order a burrito", action: #selector(AppDelegate.orderABurrito), keyEquivalent: "") statusBarMenu.addItem( withTitle: "Cancel burrito order", action: #selector(AppDelegate.cancelBurritoOrder), keyEquivalent: "") } @objc func orderABurrito() { print("Ordering a burrito!") } @objc func cancelBurritoOrder() { print("Canceling your order :(") } 

Nothing is displayed on the menu. Is there anything I need to enable to make it display the menu? I tried with images as well, it didn't work either.

3
  • The documentation for statusItemWithLength: says: "The receiver does not retain a reference to the status item, so you need to retain it. Otherwise, the object is removed from the status bar when it is deallocated." Perhaps you should try keeping a strong reference to your statusBarItem? Commented Oct 20, 2019 at 3:47
  • Thanks @TheNextMan. That's what I am doing with var statusBarItem: NSStatusItem!, but maybe it needs to be outside the class? Commented Oct 25, 2019 at 11:20
  • I never wrote any swift code before, but it appears that your statusBarItem variable drops out of scope at the end of the applicationDidFinishLaunching and will therefore be released. You need to keep a reference at the class level (a class member or property). Commented Oct 25, 2019 at 15:50

1 Answer 1

2

Funny. I did the same tutorial, and I did not have any issues.

https://8thlight.com/blog/casey-brant/2019/05/21/macos-menu-bar-extras.html

Here is what is in my code.

--

// at the top import Cocoa import SwiftUI // first lines of the class AppDelegate var window: NSWindow! // I don't think this is important var statusBarItem: NSStatusItem! // top lines of applicationDidFinishLaunching // Create the SwiftUI view that provides the window contents. let contentView = ContentView() // Menu Bar Extras let statusBar = NSStatusBar.system statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength) statusBarItem.button?.title = "🌯" let statusBarMenu = NSMenu(title: "Cap Status Bar Menu") statusBarMenu.addItem( withTitle: "Order a burrito", action: #selector(AppDelegate.orderABurrito), keyEquivalent: "" ) statusBarMenu.addItem( withTitle: "Cancel burrito order", action: #selector(AppDelegate.cancelBurritoOrder), keyEquivalent: "" ) statusBarItem.menu = statusBarMenu // the rest is window creation code // more methods of the AppDelegate // make sure this part of the code is correct to be // "superstitious"; maybe Xcode does not behave properly // with the @objc keywords/modifiers/decorators @objc func orderABurrito() { print("Ordering a burrito!") } @objc func cancelBurritoOrder() { print("Cancelling order...") } 

Otherwise, I would suggest doing the tutorial over again in case you did not see something the first time. Good luck, and happy coding.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.