61

I'm using this code to bring up my window:

[self.window makeKeyAndOrderFront:self];
[self.window setOrderedIndex:0];

But often it will be beneath other windows or displayed in other Space desktop that I last open it in. How do I make it always appear "in front" to the user?

Update I found the answer from previously asked question; make the window show up to the top of other windows: [NSApp activateIgnoringOtherApps:YES];

But how to move the app to the user's current Space?

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Teo Choong Ping
  • 12,512
  • 18
  • 64
  • 91

8 Answers8

141

To bring your app to the front:

[NSApp activateIgnoringOtherApps:YES];

Swift:

NSApp.activateIgnoringOtherApps(true)

Swift 3:

NSApp.activate(ignoringOtherApps: true)
Judge2020
  • 339
  • 4
  • 23
jtbandes
  • 115,675
  • 35
  • 233
  • 266
30

Perhaps you want:

  [self.window setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];

Experiment with the other collection behaviors... I found NSWindowCollectionBehaviorMoveToActiveSpace was a bit buggy in 10.5 but it might be better now.

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
  • 4
    How do I go about learning all these subtle things without asking? At times I feel very lost in Cocoa-land. – Teo Choong Ping Nov 16 '09 at 07:03
  • 2
    I tend to read *all* the release notes when each new OS version comes out. There's a lot of good information in there, and it only takes an hour or two. – Nicholas Riley Nov 16 '09 at 07:06
  • Read the more important programming guides and framework references, too. Start with Cocoa Fundamentals Guide and move on from there. – Peter Hosey Nov 16 '09 at 15:32
  • @NicholasRiley : I have the same problem but this answer `setCollectionBehavior` doesn't work for me in situation when there is a full-screen app, which creates a new space, my window would still show up in the original space. – Gon Oct 31 '13 at 02:50
  • Probably a bug. Spaces has never been terribly reliable or consistent, and since I wrote this, several new OS X versions have come out, each of which have substantially changed Spaces behavior. – Nicholas Riley Oct 31 '13 at 03:35
  • @Gon Have you found a solution? I have the same problem – ULazdins Dec 03 '13 at 08:52
  • @ULazdins Maybe not a good solution. But you can try `[NSApp setActivationPolicy:]` and set the _activationPolicy_ to `NSApplicationActivationPolicyAccessory` or `NSApplicationActivationPolicyProhibited `. While doing this you'll lose your app's menu and dock icon. – Gon Dec 04 '13 at 23:46
  • 2
    Hey @Gon! Seems I did find a solution (at least it did the job for me). Try out using `[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorTransient]; [self setLevel:NSPopUpMenuWindowLevel];` – ULazdins Dec 05 '13 at 08:00
  • @ULazdins :( doesn't work for me. I set my window as your code but it couldn't cover the full-screen window of other app. – Gon Dec 05 '13 at 10:54
  • @ULazdins — thanks, that combination ended up solving a problem for *me* several years later! (Floats over fullscreen apps in 10.11 and doesn't mess up when other windows spawn.) – Nicholas Riley Sep 10 '16 at 07:30
5

Swift 5.0 +

 NSWindow.orderFrontRegardless()
arthas
  • 680
  • 1
  • 8
  • 16
  • This won't work for every case. But this version is OK https://stackoverflow.com/a/1740423/5302192 – S.E. Mar 14 '22 at 00:24
  • Can you mention some cases where this won't work? – arthas Mar 14 '22 at 06:06
  • Sure. If you are building a ".bundle" package (specifically for login window etc.), you can't use that. You also need the following code line; ```swift func windowDidBecomeKey(_ notification: Notification) { guard let window = notification.object as? NSWindow else { return } guard window != self.window else { return } self.window = window } ``` – S.E. Mar 15 '22 at 15:11
4

The syntax seems to be a little different with Swift 2:

NSApplication.sharedApplication().activateIgnoringOtherApps(true)
iphaaw
  • 6,764
  • 11
  • 58
  • 83
4

Swift 3.0

NSApp.activate(ignoringOtherApps: true)
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Cee
  • 119
  • 5
3

But how to move the app to the user's current Space?

You don't. Spaces hold windows, not applications. (That's why the collectionBehavior property is on NSWindow, not NSApplication.)

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • There is a subclass of NSWindow called FloatingPanel which can be put over spaces. https://gist.github.com/jordibruin/8ae7b79a1c0ce2c355139f29990d5702 self.collectionBehavior.insert(.fullScreenAuxiliary) self.collectionBehavior.insert(.canJoinAllSpaces) – Eric_WVGG Oct 30 '21 at 19:54
1

If you want to your app to come to the front, even if its minimised and have it activated (given active focus), then use both of these:

            NSApp.activate(ignoringOtherApps: true)
            NSApp.windows.first?.orderFrontRegardless()

If you only want to bring it to the front, without giving it focus, just use:

            NSApp.windows.first?.orderFrontRegardless()
Adam
  • 1,249
  • 1
  • 10
  • 11
0

Swift 2.0

NSApp.activateIgnoringOtherApps(true)

Helpful Programming Tips and Hacks

quemeful
  • 9,542
  • 4
  • 60
  • 69