I have this problem sometimes with my dual-screen setup, especially when I'm juggling lots of things around.

Occasionally I seem to be able to move a window entirely off-screen (I'm not quite sure how) and I can't get it back. If I do a 'hide all' in the application's menu (usually it's Mail.app, so let's stick to that one for the example), everything disappears and Mail seems to think it's got an 'inbox' window in the Windows menu, but there's nothing visible. Closing and restarting the app doesn't seem to help (as the app is helpfully remembering where my window previously was) and all I end up doing is shutting down the Mac, unplugging the second monitor and restarting it, opening the application again and this seems to persuade the Mac to rethink the main window position, and all is well. I can then restart the second monitor, etc.

Is there a better way? Is there some way of marshalling windows back to a specific monitor, or a utility for moving 'open'/running windows? Am I actually doing something wrong? :-)

I'm not using Spaces, by the way - so I'm pretty sure this isn't some weird Spaces issue. :-)

A second method of arranging the Windows so you can see them (if for some reason they're staying off screen) is to change to the
application that owns the window in question, hold the Option key down
and choose "Arrange in Front" from the Window menu. It will then
arrange all the windows of that application in a cascade from the top
left of the screen.

Oh, brilliant - thanks! I've upvoted his answer there, and given you some credit here for pointing me to the right place. There are some good suggestions there, and I almost can't wait for the problem to occur again now. :-)
–
robsoftAug 27 '10 at 10:07

This solution doesn't work in Yosemite, you can only access the Window menu from the display that the window is on in Yosemite.
–
ShemSegerMay 7 at 15:02

When a monitor is unplugged (including the adapter as well if applicable - leaving the adapter leaves OS X thinking the monitor is still plugged in) all the Windows should move onto your main screen. Occasionally some windows (eg. Firefox) will keep their position on the very far right of the screen leaving you just enough room to grab the title bar and move it where you'd like to.

One method is before you unplug the external monitor (or before you unplug the dongle) is to open up the Displays Preference Pane in System Preferences and click on the "Gather Windows" button. This is supposed to bring every window on the main screen and the option does not appear if you don't have a second monitor plugged in.

A second method of arranging the Windows so you can see them (if for some reason they're staying off screen) is to change to the application that owns the window in question, hold the Option key down and choose "Arrange in Front" from the Window menu. It will then arrange all the windows of that application in a cascade from the top left of the screen.

Lastly, there are several AppleScripts available (eg. this one at Snipplr) that will also grab every window off screen and move it on screen for you.

There is also a nifty shareware app called Stay that will help automate a lot of this for you, while there is also MarcoPolo which will do much more than just notice if a monitor is disconnected and allows you to run scripts (in case you want to do more than just rearrange the windows).

Did you find any solution for xquartz?
–
gebirgsbärbelApr 17 '14 at 10:08

The solution I found was to connect an external monitor: in that case, the window changed is position in a way I could move it. I move it so that, after disconnecting the screen, the window top bar is "accessible".
–
lauhubApr 17 '14 at 10:10

@gebirgsbärbel See the answer I just posted below. I think you may find it useful.
–
nukeguyApr 9 at 13:37

I have two screens and loading Inkscape put it somewhere inaccessible. I could see it by using "show all windows", but if I selected it from there it went and disappeared again. The solution I found came from this mailing list comment.

When unplugging the second screen I could see Inkscape. But then even after moving it around a little, if I plugged the other screen back in it disappears off the edge again. The solution is to unplug the second screen, grab the title bar of the window and while still grabbing it plug the monitor back in. (!)

Also, for completeness, I came across this: Stay app for Mac although I haven't tried it or bought it yet.

From their website:

Stay can store a set of windows for every combination of displays that you use with your computer. For example, if you have a laptop that is sometimes connected to an external monitor, you can store two sets of windows in Stay; one with the external monitor connected and one without. This done, you’ll be able to ensure that your windows are always where you want them to be by having Stay restore windows whenever you connect or disconnect the external monitor. Stay can even be configured to automatically restore windows as displays are connected and disconnected.

I have a solution that worked for me and I trawled forums and tried everything I could possible.

In the end I found a way. Install an app called BetterTouchTool. This allows you to make a shortcut to do various handy things with your screen etc. Then setup an app specific shortcut (much easier than it sounds), and make it simple. I set it so that a "two finger swipe- up" led to a "Maximise window" action. This stuck the missing application on to my laptop screen and away from the phantom monitor screen it had been stuck on.

Problem solved.

Here's an image of what BetterTouchTool looked like when I'd set the shortcut up:

You can create a simple app to do it in the AppleScript Editor. Enter the following text and save it as an app. If you put it in your Applications folder it will be easy to access via SpotLight. I named mine gather windows.

property processesToIgnore : {}
tell application "Finder"
set _b to bounds of window of desktop
set screen_width to item 3 of _b
set screen_height to item 4 of _b
end tell
tell application "System Events"
set allProcesses to application processes
set _results to ""
repeat with i from 1 to count allProcesses
set doIt to 1
repeat with z from 1 to count processesToIgnore
if process i = process (item z of processesToIgnore) then
set doIt to 0
end if
end repeat
if doIt = 1 then
tell process i
repeat with x from 1 to (count windows)
set winPos to position of window x
set _x to item 1 of winPos
set _y to item 2 of winPos
if (_x < 0 or _y < 0 or _x > screen_width or _y > screen_height) then
set position of window x to {0, 22}
end if
end repeat
end tell
end if
end repeat
end tell

I thought I should make an answer to this that describes solutions specifically for XQuartz. Many of the solutions here do not work for XQuartz. From this ticket: https://xquartz.macosforge.org/trac/ticket/796 , I've found two solutions that work and I feel it would be beneficial to state them here.

If you can unplug/plug-in your monitor easily, then unplug your monitor so that your window appears in a spot that you can access. Then, with your mouse, click on the window and hold down the mouse button as if you are going to drag it. Without letting go of your mouse or the window, plug in your monitor. The XQuartz window should stay where your mouse is.