Open app in the active screen

If I launch my app from my second screen, it opens in the first screen. How do I make it open in the screen that is currently active?

Check screen(1) available left and position your window.left relative to it.

Screen(1).AvailableLeft returns the same number regardless of the screen I open the app in.

Check System.MouseX to see where your mouse cursor is as you opened the app. If it is within the boundaries of screen(1), then the active screen is screen(1). Same principle for multiple screens.

That’s a pretty good idea. Only problem is if the app is opened with a global keyboard shortcut and the pointer happens to be in the unactive screen.

How do you define “active screen” ?

I would think it’s the one that doesn’t have a dimmed menu bar.

I would ask Apple with that question…

Since Mavericks, I retain pullin hair from my head because of their ■■■■■ usage of a second monitor.

the cursor is never where I expect it to be when I cmd-tab… a.k.a. the active screen is never where I feel it is;

also, I do not think Apple knows anymore what is an Extended Desktop (when you have more tha one installed monitor / I use DisplayPort for the second screen).

Most of the apps in my Applications folder open on the screen where I launch them. Xojo made app always opens in the first screen.

What you need to know is the mainScreen, which is provided by the NSScreen object

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSScreen_Class/Reference/Reference.html#//apple_ref/occ/clm/NSScreen/mainScreen

Excerpt :

[code]mainScreen
Returns the NSScreen object containing the window with the keyboard focus.

  • (NSScreen *)mainScreen
    Return Value
    The main screen object.

Discussion
The main screen is not necessarily the same screen that contains the menu bar or has its origin at (0, 0). The main screen refers to the screen containing the window that is currently receiving keyboard events. It is the main screen because it is the one with which the user is most likely interacting.

The screen containing the menu bar is always the first object (index 0) in the array returned by the screens method.

[/code]

MacOSLib uses calls to NSScreen, so it should be relatively easy to get mainScreen, and then decide where to display the app windows.

Thanks Michel.

I still think that this shouldn’t be something that has to be coded in the app but should be default behavior. That’s how a test app behaved when I build it with Xcode.

Fair enough, but my intention was not to tell you what Xojo apps should do, rather to offer a possible solution.

Why not use Feedback to tell Xojo you think apps should behave more like your Objective-C test program ?

Better late than never :wink:

43049 - Ability to launch app on external monitor
<https://xojo.com/issue/43049>