OS X 10.10 Yosemite - Adding a menulet

2019-04-13 14:51发布

问题:

I am new to OSX programming and recently started a demo project for OS X 10.10. Found this -> http://cocoatutorial.grapewave.com/tag/menulet/ good tutorial to add menulet in OSX status bar. The problem is My project is using swift language and the methods and project structure/files are somewhat different. I would like to know if anyone has successfully tried this on Yosemite? Thanks.

Edit: Specific question would be how do i replace awakefromnib method to work with present AppDelegate.swift syntax?

回答1:

The whole thing is this...

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow // Remove this and delete window in IB to remove window
    // ... also, remove MainMenu from IB.
    @IBOutlet var statusMenu: NSMenu
    var statusItem: NSStatusItem? = nil

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }

    override func awakeFromNib() {
        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(NSVariableStatusItemLength))
        self.statusItem!.menu = self.statusMenu
        self.statusItem!.title = "Status"
        self.statusItem!.highlightMode = true
    }

    @IBAction func doSomethingWithMenuSelection(sender : AnyObject) {
        println("Action pressed")
    }

}

I just copied it from your link, and translated to Swift. It still shows a window, etc., which should be trivial to remove... UPDATE showed how...

(and of course I'm running it on Yosemite)