0

I'm creating an application where a menubar seems to be the most convenient way to have the user's desktop clean without a window. I've seen many tutorials online and on stack overflow but they seem to be only for Objective-C. I only use Swift. If you don't know what a menubar is, they're these icons:

enter image description here

I would like my app to have one of these instead of a constant full window. And if I can, how can I have a button on my menubar that brings up the window. Lastly, how can I have my icon not show, but I still have the finder advantages. (Like File, Edit..). For example,

enter image description here

I have already tried to put

Application is Agent (UIElement) to False

in my Info.plist but that also takes away my finder advantages.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Jack
  • 955
  • 1
  • 9
  • 30
  • "I've seen many tutorials online and on stack overflow but they seem to be only for Objective-C. I only use Swift" So what? This has nothing to do with _language_. Use the tutorials you've found. – matt Jan 17 '16 at 16:28
  • What does "Finder advantages" mean? – rob mayoff Jan 17 '16 at 16:28
  • 1
    @Matt the tutorials I can't understand because they are in a completely different language. I can't code in something I don't understand if I want to make a useful application. – Jack Jan 17 '16 at 16:29
  • @rob mayoff Editting now. – Jack Jan 17 '16 at 16:29
  • 1
    I still don't understand what "Finder advantages" means. You've added a picture of the Safari menu bar. Safari is not Finder, and a menu bar is not "advantages". – rob mayoff Jan 17 '16 at 16:32
  • @rob mayor Safari has those advantages. For me, I would like the user to be able to use File, Edit, and stuff like that. Maybe I used a bad term for it, but I didn't really know what they were called. :) – Jack Jan 17 '16 at 16:34
  • "the tutorials I can't understand because they are in a completely different language. I can't code in something I don't understand if I want to make a useful application" You don't have to code in it. But you can surely _read_ it. If you can't, learn. It takes about three minutes to learn to read Objective-C code (especially if you already know Swift). – matt Jan 17 '16 at 16:36
  • I'm not exactly the greatest at swift and was never able to wrap my head around obj c. Some things I just don't know how to convert. I can't be expected to understand something I don't. That's why I asked this question... – Jack Jan 17 '16 at 16:37
  • Dear Jack, the beginning is always hard and painful. The best way how to learn something is trying to write something very simple and next continue deeper and deeper, as your knowledges increasing. The same application can be written in different programming languages, but user interface of your OSX application should follow some rules and recommendations. If i understand, you use Xcode for your development. All documentation is part of Xcode installation. Read it first! – user3441734 Jan 17 '16 at 16:59
  • Thanks for the tip but I kinda already got through the that part. I'm mainly an iOS developer, but decided to try to make an OS X application since I know swift. My main problem is that I seriously can't understand obj c for the life of me. I have researched for a full summer and got no where. Then I heard of swift and learnt it. I'm not asking to learn obj c, I just want to know how to make a menu bar application. Thanks for the recommendation though! :) – Jack Jan 17 '16 at 17:02
  • Consider that a menu bar application usually has **no** main menu, doesn't appear in the dock and is most of the time in the background. You have to implement the whole functionality yourself inclusive your "Finder advantages" and the application does not respond to keyboard shortcuts as long as the menu is not opened. – vadian Jan 17 '16 at 17:11
  • Is here any way I can have the finder advantages only when the window is selected? For example, I want the menu bar to have an option to open the window, and I want the window to have the advantages. If it isn't possible, I will try something else. – Jack Jan 17 '16 at 17:16

1 Answers1

8

Presumably what you're saying is that you want the text editing items (like Undo, Cut, Copy, Paste, Select All) from the Edit menu to work in your app's window.

Those menu items are part of another application, and only send messages in that application. They aren't available to your application, regardless of whether it's an “agent” (with no visible menu bar of its own). If one of your agent app's windows is the key window and the user clicks on a menu title (like File or Edit) that belongs to another app, then that app will activate and your app's window will “resign” the key window status.

You can make the usual shortcut keys (like ⌘X for Cut) work for your app, and it's easy. When one of your app's windows is the user's key window, your app receives keyboard events, and your NSApplication object (created for you automatically) will check its mainMenu for keyboard shortcuts even though the main menu is not displayed on the screen.

The OS X “Cocoa Application” project template sets up a main menu bar for you in MainMenu.xib (or in Main.storyboard), with all of the menu items wired up to the appropriate actions. So if you keep that main menu bar and the Edit menu and the menu items in the Edit menu and leave the shortcuts set on those items, then the keyboard shortcuts will work even if you set LSUIElement to YES in your Info.plist, when one of your app's windows is the key window. In other words, the shortcut keys will work by default, and you have to change things to make them stop working.

Text fields in your app's windows will also still get the default right-click menu with the usual items like Cut, Copy, and Paste, so you don't need to do anything else to make that work either.

Here's the contents of my test app's MainMenu.xib:

MainMenu.xib

I've left the main menu bar alone. I've created a separate menu with two items, “Show Window” and “Quit”. I've set the shortcut for “Quit” to ⌘Q, but this shortcut has no effect. The StatusItem > Quit menu item (not visible in my screen shot) off the main menu bar has the same shortcut set, and that's the setting that matters. I've set the shortcut on this other Quit item because it's visible to the user, and the main menu bar won't be visible to the user.

I've wired this Quit item to the terminate: action of First Responder. (The StatusItem > Quit menu item is connected the same way by default.)

Here's my AppDelegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow!
    @IBOutlet var statusItemMenu: NSMenu!

    var statusItem: NSStatusItem?

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
        let statusItem = self.statusItem!
        let button = statusItem.button!
        button.title = "Hello"
        statusItem.menu = statusItemMenu
    }

    @IBAction func showWindow(sender: AnyObject) {
        NSApp.activateIgnoringOtherApps(true)
        window.makeKeyAndOrderFront(sender)
    }

}

I've wired the “Show Window” menu item to the showWindow(_:) action, and I've connected the statusItemMenu outlet to that standalone menu in the XIB.

I also set “Application is Agent (UIElement)” to “YES” in Info.plist.

When I run this app, it creates the status item in the menu bar. I can choose “Show Window” from the item and my window comes to the front and becomes key. I can right-click the text field to get its context menu. I can use the standard shortcuts to cut/copy/paste/etc., to close the window, and even to quit the app.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thank You! My application doesn't have a MainMenu.xib file but has it in the Main.Storyboard. Is there a way I can transfer it because I want my work to be cleaner. Also, how would I be able to replace 'Hello' with an image? Again, thank you, thank you, thank you! – Jack Jan 17 '16 at 20:50
  • 1
    Check out [this tutorial](http://www.raywenderlich.com/98178/os-x-tutorial-menus-popovers-menu-bar-apps). – rob mayoff Jan 17 '16 at 21:27