<p>Whenever I use my MacBook away from my desk and later plug it into an external display (as primary), I get into the state of having windows deposited in both the notebook monitor and the external one.</p>

<p>To move all windows to a single screen, my current solution is to "Turn on mirroring" in the display preferences and then turn it off again. This is rather tedious, though. Does anyone know of a better way?</p>

<hr>

<p>I'm afraid the script posted by @<a href="https://stackoverflow.com/questions/39066/move-all-windows-to-a-single-monitor-with-two-attached-in-mac-os-x#39097">erlando</a> does absolutely nothing for me, running Mac OS X 10.5.4. (I.e., with windows on both screens, running the script moves not a single one of them, and it does not return any errors.) I guess I'll just have to stick with using the "mirror/unmirror" method mentioned above.</p>

<hr>

<p>@<a href="https://stackoverflow.com/questions/39066/move-all-windows-to-a-single-monitor-with-two-attached-in-mac-os-x#47233">Denton</a>: I'm afraid those links provide scripts for getting windows which are orphaned from <em>any</em> screen back onto the display. I ‘just’ want to move all windows from a secondary display onto the primary display.</p>

[{'answer_id': 39097, 'author': 'erlando', 'author_id': 4192, 'author_profile': 'https://Stackoverflow.com/users/4192', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a command-line script to do just that: <a href="http://zach.in.tu-clausthal.de/software/" rel="nofollow noreferrer">http://zach.in.tu-clausthal.de/software/</a>.</p>\n<p>It\'s a little down the page under &quot;Move Off-Screen Windows to the Main Screen&quot;.</p>\n<hr />\n<pre><code>-- Source: http://www.jonathanlaliberte.com/2007/10/19/move-all-windows-to-your-main-screen/\n-- and: http://www.macosxhints.com/article.php?story=2007102012424539\n--\n-- Improvements:\n-- +  code is more efficient and more elegant now\n-- + windows are moved also, if they are &quot;almost&quot; completely off-screen \n--      (in the orig. version, they would be moved only if they were completely off-screen)\n-- + windows are moved (if they are moved) to their closest position on-screen\n--     (in the orig. version, they would be moved to a &quot;home position&quot; (0,22) )\n-- Gabriel Zachmann, Jan 2008\n\n-- Example list of processes to ignore: {&quot;xGestures&quot;} or {&quot;xGestures&quot;, &quot;OtherApp&quot;, ...}\nproperty processesToIgnore : {&quot;Typinator&quot;}\n\n-- Get the size of the Display(s), only useful if there is one display\n-- otherwise it will grab the total size of both displays\ntell application &quot;Finder&quot;\n    set _b to bounds of window of desktop\n    set screen_width to item 3 of _b\n    set screen_height to item 4 of _b\nend tell\n\ntell application &quot;System Events&quot;\n    set allProcesses to application processes\n    repeat with i from 1 to count allProcesses\n        --display dialog (name of (process i)) as string\n        if not (processesToIgnore contains ((name of (process i)) as string)) then\n            try\n                tell process i\n                    repeat with x from 1 to (count windows)\n                        set winPos to position of window x\n                        set _x to item 1 of winPos\n                        set _y to item 2 of winPos\n                        set winSize to size of window x\n                        set _w to item 1 of winSize\n                        set _h to item 2 of winSize\n                        --display dialog (name as string) &amp; &quot; - width: &quot; &amp; (_w as string) &amp; &quot; height: &quot; &amp; (_h as string)\n                        \n                        if (_x + _w &lt; 40 or _y + _h &lt; 50 or _x &gt; screen_width - 40 or _y &gt; screen_height - 40) then\n                            \n                            if (_x + _w &lt; 40) then set _x to 0\n                            if (_y + _h &lt; 50) then set _y to 22\n                            if (_x &gt; screen_width - 40) then\n                                set _x to screen_width - _w\n                                if (_x &lt; 0) then set _x to 0\n                            end if\n                            if (_y &gt; screen_height - 40) then\n                                set _y to screen_height - _h\n                                if (_y &lt; 22) then set _y to 22\n                            end if\n                            set position of window x to {_x, _y}\n                            \n                        end if\n                    end repeat\n                    \n                end tell\n            end try\n        end if\n    end repeat\nend tell\n</code></pre>\n'}, {'answer_id': 47233, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 2, 'selected': False, 'text': '<p>There is an article on using AppleScript to do this at <A HREF="http://www.macosxtips.co.uk/index_files/move-all-windows-to-main-display.html" rel="nofollow noreferrer">macosxtips.co.uk</A>, and another at <A HREF="http://www.macosxhints.com/article.php?story=2007102012424539" rel="nofollow noreferrer">macosxhints.com</A>.</p>\n'}, {'answer_id': 75475, 'author': 'Jesús A. Álvarez', 'author_id': 13186, 'author_profile': 'https://Stackoverflow.com/users/13186', 'pm_score': 2, 'selected': False, 'text': '<p>You can click the "Gather Windows" button in the Displays preference pane.</p>\n'}, {'answer_id': 10303502, 'author': 'Jo Liss', 'author_id': 525872, 'author_profile': 'https://Stackoverflow.com/users/525872', 'pm_score': 2, 'selected': False, 'text': '<p>As you said, the best answer seems to be turning "Mirror Displays" on and off again. Afterwards, all windows will have been collected on the main screen, and the secondary screen will be empty.</p>\n\n<p><img src="https://i.stack.imgur.com/gst5c.png" alt="screenshot"></p>\n\n<p>This is a bit cumbersome, but nothing else has worked for me in Lion.</p>\n'}, {'answer_id': 10411943, 'author': 'Ben B', 'author_id': 1369775, 'author_profile': 'https://Stackoverflow.com/users/1369775', 'pm_score': 3, 'selected': False, 'text': '<p>On Lion you can toggle Mirror Displays using <kbd>fn</kbd>+<kbd>Cmd</kbd>+<kbd>F1</kbd> (provided you are using the media control keys as default).</p>\n\n<p>This also works on Snow Leopard and likely everything in between, also possibly further back.</p>\n'}, {'answer_id': 11380305, 'author': 'failrat', 'author_id': 1509514, 'author_profile': 'https://Stackoverflow.com/users/1509514', 'pm_score': 5, 'selected': False, 'text': "<p><code>Cmd+F1</code> appears to be a Mirror Displays shortcut in Snow Leopard. Don't know about Lion, etc, though.</p>\n\n<p>Just tap it twice and see what happens (-:</p>\n\n<p>For the people who prefer to set up their function keys to act in the old-fashioned way (not as brightness/sound controls etc.), it will be <code>Cmd+Fn+F1</code></p>\n"}]
['https://Stackoverflow.com/questions/39066', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4161/']