Nathan H
Nathan H

Reputation: 49371

Cocoa put window in the foreground

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:

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.

Upvotes: 1

Views: 1565

Answers (2)

CRD
CRD

Reputation: 53000

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"...

Upvotes: 1

sidyll
sidyll

Reputation: 59277

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.

Upvotes: 5

Related Questions