0
votes

I am building a Cocoa Mac application that runs in the background but has a main window.

To make it run in the background I've set "Application is background only" to "YES".

I built a system tray:

system tray

If you close the main window, you can re-open it by clicking "open".

I have however some issues with the layering of windows:

  • When I start the application, the main window opens however it appears in the background, behind any other windows or applications I have open at the time.
  • Clicking on "open" doesn't bring the window to the foreground. It opens it correctly if it was closed, however it stays behind any windows.
  • Clicking on preferences or about has the same issue. It opens the correct window but it appears behind any other windows.
  • On my main window there is a textfield. I can click on it, the cursor blinks as if I am ready to type. But when I type it actually types in some other background window! For example if I have TextWrangler open in the background, it will type there instead of the textfield...

Here is my code for handling the "open":

- (IBAction)show:(id)sender {
    [NSApp activateIgnoringOtherApps:YES];
    [window makeKeyAndOrderFront:sender];   
}

Note that IF I set "Application is background only" to "NO" (which means I have a dock icon appearing), then clicking on "open" brings the window to the foreground as expected. And typing in the textfield works as expected.

2

2 Answers

4
votes

Instead of background only, I think you want Application is agent (UIElement) set to YES. Background only is for application not intended to be visible for users.

1
votes

Background only (LSBackgroundOnly YES) is intended for faceless background applications, Accessory (LSUIElement YES) is intended for background applications with a UI and status menu (menu extra/accessory menu/etc. - the name changes...).

An accessory will not appear in the dock, have a standard menu bar, or appear in the Finder's Force Quit dialog. It can be "active" and can have the key window.

Though it does not have a standard menu bar bizarrely (maybe a bug) if a MainMenu is declared in the XIB then it will respond to key shortcuts when it is active. To avoid this make sure you have no MainMenu or use [NSApp setMainMenu:nil] when you wish to disable the shortcuts.

The whole background/accessory/application/active/etc. area is not exactly well-defined, be prepared for "fun"...