My Menu Bar Extra Item does not display. Why?

Hello:


I am creating a Menu Bar Extra Item for my MAC os App. I have the following code but nothing displays on the menu bar. W ould appreciate some advice on where in the code I need to make changes:



import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
   
//@IBOutlet weak var window: NSWindow!
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
//   var item: NSStatusItem? = nil
    @objc func QuitScorCentMasterReview(_ sender: Any?) {
          NSApplication.shared.terminate(self)
        }
    func applicationDidFinishLaunching(_ aNotification: Notification) {
       
        if let button = statusItem.button {
          button.image = NSImage(named:NSImage.Name("SMR_icon.png"))
          button.action = #selector(QuitScorCentMasterReview(_:))
        }
       
        func constructMenu() {
          let menu = NSMenu()

    
          menu.addItem(NSMenuItem(title: "End the Application", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))

            statusItem.menu = menu
        }
       
        constructMenu()
       
    }

Accepted Reply

Did you update the info.plist ?


have a look here


h ttps://www.appcoda.com/macos-status-bar-apps/

Replies

Did you update the info.plist ?


have a look here


h ttps://www.appcoda.com/macos-status-bar-apps/

Hello Claude:


I did make the .plist file changes. What I just discovered is :


If I change the line with image file line 15 to a line with item title as so:

statusItem.button?.title = "This is a test"


Then the text shows up in the status bar. But Apple wants only an image item.

How can I create an image that is acceptable to Xcode?

I found out the problem.


I had the file extension .png in my code ("SMR_icon.png". When I remove the .png it worked.

Thanks for feedback. Effectively, the name must be given without extension.


Don't forget to close the thread.

Just one more question before I close:


I use this code to close my app, What is the code to implement opening the app using the same icon?


menu.addItem(NSMenuItem(title: "Close ScorCent Master Review", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))

What do you mean by same icon ?


Did you try to use a terminal command ?

open -a ApplicationName


Have a look here to see how to run terminal command from Swift code

https://stackoverflow.com/questions/43014600/how-to-execute-terminal-command-in-swift

I had a similar issue with SwiftUI but I was calling the image correctly

I was declaring the statusItem inside of didFinishLaunching as there were some bugs

The solution was the declare the statusItem as an optional out side didFInishLaunching