Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X Menubar application: How to bring window to front

I am developing a menubar-only application for OS X and I am struggeling to get a settings window to show up in front of other apps.

App setup

"Menubar-only application" means:

  • I removed the "Is Initial Controller" from the NSWindowController in the main storyboard file. The main storyboard's window is not used in my app
  • I added an NSMenu to the "Application Scene" in the main storyboard. This will become my menubar menu
  • I set LSUIElement to YES to hide the dock icon
  • I set LSBackgroundOnly to NO (see NSWindow makeKeyAndOrderFront makes window appear, but not Key or Front)

When the app starts, I create an NSStatusItem and add the NSMenu from the storyboard as its menu. This all works fine - the app starts, shows no window and no dock icon but a menubar item that contains the menu from the storyboard.

Settings window

I now wanted to add a settings window that is shown when a menubar entry is clicked. I therefore:

  • Created a new .xib-file and added an NSWindow to it
  • Created a custom NSWindowController that connects the outlets and actions
  • Instantiated the custom NSWindowController using initWithNibNamed: on app launch

When the "Settings"-entry from the menu is clicked, I then try to bring the settings window to front using:

[self.settingsWindowController.window center];
[self.settingsWindowController.window showWindow:self];
[self.settingsWindowController.window makeKeyAndOrderFront:NSApp];

The window is shown, but not brought to the front but rather hidden behind other apps.

Any ideas how to solve this? Thanks!

like image 248
BlackWolf Avatar asked Mar 29 '15 10:03

BlackWolf


People also ask

How do I move an application to the front on a Mac?

In "Classic" mode, clicking on a window brings all the windows in that app to the front, just like it did in classic Mac OS. In "Modern" mode, only the clicked window comes to the front. In either mode, Shift-click on a window to get the opposite of the chosen behavior. Save this answer.

How do you keep a window in front on a Mac?

Other Ways to Keep Your Application Window “Always On Top” First, though, head to the System Preferences screen and choose Mission Control. Here, check that “Displays have separate Spaces” is active, then open some apps. With the toolbar of one app, hover over the green window button.

How do you move a window on a Mac?

Move a window to one side of the screen: Press and hold the Option key while you move the pointer over the green button in the top-left corner of the window, then choose Move Window to Left Side of Screen or Move Window to Right Side of Screen from the menu that appears.


1 Answers

You need to call:

[NSApp activateIgnoringOtherApps:YES];

(This is one of the rare occasions where it's correct to pass YES to that method.)

For Swift you can use

NSApp.activate(ignoringOtherApps: true)
like image 93
Ken Thomases Avatar answered Sep 23 '22 05:09

Ken Thomases