4

In a Mac app how can I open a NSWindow on a specific NSScreen (let's say the second screen)?

This is how I show the window, but it only shows on the main screen

self.windowController = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("mainWindow") as! NSWindowController
let window = self.windowController.window!
window.makeKeyAndOrderFront(self)

Answers in both Swift and OC are welcome.

hklel
  • 1,624
  • 23
  • 45
  • I am pretty sure the OS likes to treat multiple screens as "one big screen" so you could control that with position. See here http://stackoverflow.com/questions/16658582/how-to-move-nswindow-to-a-particular-screen – Justin Meiners Nov 14 '15 at 16:31
  • @JustinMeiners Thanks! I will try it out. – hklel Nov 14 '15 at 16:39

1 Answers1

3

Use the class function 'screens' to get an array of all the screens you have. From the array, pick the screen you want your window to appear on. Use the co-ordinates on that window (which are relative to the main window) to make a rect for your new window like this;

    [self.window setFrame:CGRectMake(pos.x, pos.y, [mywindow frame].size.width     
        , [mywindow frame].size.height) display:YES];

where pos is computed from the array of screens and your selectoin.

john elemans
  • 2,578
  • 2
  • 15
  • 26