3

I'm trying to get a mac app to open on the current desktop when I use a a global keyboard shortcut (that I've overriden manually in the app). However, when I perform the keyboard shortcut, my desktop switches and shows me the app on the desktop it was previously on instead of the current one. The app is active, but on that other desktop.

Currently, I'm calling:

window?.makeKeyAndOrderFront(nil)
NSApplication.shared.activate(ignoringOtherApps: true)

Thanks in advance for the help.

Shekar
  • 240
  • 1
  • 5
  • 14
  • Are the current and previous windows yours? Is the keyboard shortcut global? Is the current window the frontmost window? Is the app active? Please edit the question. – Willeke Feb 06 '21 at 08:57
  • @Willeke i edited the question, although I'm not entirely sure what you mean by "are the current and previous windows yours" and "is the current window the frontmost window." I changed the question to talk about "desktops" instead of "windows" because I think that may be more clear. – Shekar Feb 06 '21 at 15:25
  • Calling a space "window" is very confusing. – Willeke Feb 06 '21 at 15:58

1 Answers1

1

In Interface Builder, select the window in question, and in the Attributes Inspector, change Spaces from Inferred Behavior to Move To Active Space.

That should allow the window to be shown on the currently active space rather than switching back to whichever space it was shown on last.

You can also do this programmatically by modifying the window's collectionBehavior:

window.collectionBehavior.insert(.moveToActiveSpace)
NSGod
  • 22,699
  • 3
  • 58
  • 66
  • Thanks, that worked! However, when I navigate away from the space and come back, the app is placed behind the previously active app. Is there a way to prevent that? I added the line you suggested before and after the two lines in my original question, and both ways resulted in the aforementioned behavior. – Shekar Feb 06 '21 at 20:46