Move All Windows To Your Main Screen…

ApplescriptSo, you’ve got more than one screen? Life is good with all that wonderful screen real estate, then BAM… You end up back with just your main screen, but your applications have left their windows on those other displays, far out of your reach…

Let’s fix that. This little script simply goes and tells all of your applications to get their act together, to move their window’s to the main display.

You’ll need access for assistive devices turned on, which you can do by going in to System Preferences, Universal Access. At the bottom check the box for “Enable access for assistive devices”.

As it is, the script will tell all applications currently running, that if they have a window that is entirely on the main display – to move it to the top left of the screen.

You may have some applications which keep windows off screen on purpose, if you experience any strange behavior with these applications you can exclude them from the applications which get checked by adding their name to the “processesToIgnore” list at the top of the script. There is an example to show how the list should look in the comment right above it. Be sure to use the exact name of the application.

Lastly, if you have more then one display attached when this script is ran it will basically do the same thing, but windows on other screens will not be moved to the main display, only windows outside the range of all current displays.

29 thoughts on “Move All Windows To Your Main Screen…

  1. Brilliant idea. Could you write a script that would gather all the windows to the main display, even if there are other displays attached?

  2. AFAIK, there is no way to get the size of the main display with Applescript (not easily anyway). The current Finder block gets the size of the desktop, which is going to be the size of all connected displays. The following change would accomplish what you are asking for though:

    At the top of the script, remove the Finder tell block:
    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

    replace it with:
    set screen_width to
    set screen_height to

    So, if your screen resolution is 1440×900, you would have
    set screen_width to 1440
    set screen_height to 900

  3. Thanks for that, because it did give me something useful. I made a copy of your script, called “gather windows to main display”, and used your suggested code block, except I set it for 800 and 600, so that the windows definitely cluster near the top left corner, no matter what screen res I choose [I change that setting often]. Great! Thanks!

    I renamed the original version to “move offscreen windows to the main display” so I won’t be confused later — I often run my MBP attached to a 2nd monitor.

  4. Pingback: Life, it is a Travesty… » links for 2007-10-24

  5. Thanks – does just what I need (once I made the fix mooncaine asked for). And I can confirm it works on Leopard (in case there was any doubt).

    Dumb question I know but how can I make the script into a mini-app so when I double-click on it it runs rather than launching the editor from which I have to choose “run”?

  6. Thanks for this – terrific solution to my problem. I normally run Mail.app on my second monitor, but haven’t found a VNC client that will let me access it remotely. When I’m out of town and forgot to push Mail over to the main monitor before I left, I’ve been stuck. Your script works great (though I had to use the modification to set screen size).

  7. You rock. This is one of those occasional areas where Mac OS sucks compared to Windows in not having a native solution for this issue, but this script takes care of that — thanks!!

  8. Could this be modified to save the window positions and the restore them later?

    This would be for the situation where your MBP is connected with an external monitor with apps everywhere on both. Then you sleep your MBP, disconnect the external, and you decide you need to open it without the external connected. All your apps get shoved to the laptop screen – annoying but understandable. Then you sleep it again, come back to your external monitor and wake up your MBP. How to get all those apps (times how many ever Spaces you use…) back to the external monitor without moving them all manually?

    Could there be a Restore Window Position script?

  9. This script saved me when the stoopid iPhone emulator ‘fell off’ my main screen never to be retrieved – thanks!

  10. I am a complete novice to using scripts but found this site because i’m having the same problem with a window stuck off screen. when i try to run the script it gives an error message saying:

    “can’t get bounds of window of desktop”

    and it highlights the ‘bounds’ in the line:
    set _b to bounds of window of desktop

    can you help?

  11. @Jonathan Laliberte

    This is a pretty slick idea. Could this idea be extended to iterate through all open applications, generically, and their child windows? In my case I am running Spaces (6 of them) and have work and projects setup up in each – not necessarily all the same all the time…

    I will try it but I am not sure I am Applescript-savvy enough…

  12. JR: It should be possible. Unfortunately, I don’t have the time to make changes to the script. If you end up advancing it, let me know and I’ll link to your changes.

  13. Here is an applescript that does not require assistive devices to be on:

    tell application “System Preferences”
    set current pane to pane id “com.apple.preference.displays”
    reveal (first anchor of current pane whose name is “displaysArrangementTab”)
    tell application “System Events” to tell process “System Preferences”
    set frontmost to true
    click checkbox “Recopie vidéo” of group 1 of tab group 1 of front window
    end tell
    set current pane to pane id “com.apple.preference.displays”
    reveal (first anchor of current pane whose name is “displaysArrangementTab”)
    tell application “System Events” to tell process “System Preferences”
    set frontmost to true
    click checkbox “Recopie vidéo” of group 1 of tab group 1 of front window
    end tell
    end tell
    property brightness_level : 0.0
    tell application “System Preferences”
    activate
    set current pane to pane “com.apple.preference.displays”
    tell application “System Events”
    set value of slider 1 of group 2 of tab group 1 of window “LCD couleur” of process “System Preferences” to brightness_level
    end tell
    quit
    end tell

    Be sure to replace “LCD couleur” with “color LCD” and “Recopie vidéo” with “Mirror displays” for English language installations of MacOS.

    The last 8 lines of code turn the laptop display brightness all the way down, as I like to do when I work with an external monitor. If you would rather keep the laptop monitor active, just delete those lines of code.

    Enjoy!

  14. Here’s a variation that preserves the y value … though is hardcoded for a wide monitor on the LEFT. I also hardcoded for a single app.

    tell application “System Events”

    tell process “Safari”

    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 > 1920 then
    name of window x
    set position of window x to {_x – 1920, _y}
    end if
    end repeat

    end tell
    end tell

  15. I tested this with a 3 display setup on Snow Leopard. I was hoping it would gather all windows on all 3 displays to my center display (my main display) where my dock resides. It didn’t work for me until I replaced the Finder tell block with the “set_screen” block as mentioned in this post above:

    Jonathan Laliberte :
    AFAIK, there is no way to get the size of the main display with Applescript (not easily anyway). The current Finder block gets the size of the desktop, which is going to be the size of all connected displays. The following change would accomplish what you are asking for though:
    At the top of the script, remove the Finder tell block:
    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
    replace it with:
    set screen_width to
    set screen_height to
    So, if your screen resolution is 1440×900, you would have
    set screen_width to 1440
    set screen_height to 900

    After doing that, it works for me perfectly. Any windows placed anywhere on my three displays will all magically transport themselves to the screen that contains my dock.

    FYI, I found this exact same script (verbatim) on another blog. http://www.leonamarant.com/2008/04/02/how-to-get-off-screen-windows-back-on-your-mac-os-x-v105/#comments

    I’m not sure who the real author is, but that blog didn’t have the solution to modify the script for 3 displays, and the blog here did.

    So thank you to the original author, whomever that may be. I’m very happy now.

  16. Pingback: Recovering off screen windows in Mac OS X | Tony Amoyal

  17. Thanks for that awesome script! It works great in my 3 display setup in connection with SwitchResX. Cheers, Mike

  18. Thx! It really helped, I have been stuck with this problem for years, very happy 😉

  19. Hi,

    is there an update of this script (or does anyone have a suggestion for Lion ? (OS X 10.7)

    I have “Enable access for assistive devices” checked in my Universal Access system preferences… but, upon running the script it gives me an “System Events got an Error: access for assitive devices is disabled.”

    any help?

    -Alf

  20. I found this useful. I wanted to mention that you can get the resolution of your desktop in applescript without hardcoding the resolution using the following:

    set command to “/usr/sbin/system_profiler SPDisplaysDataType | grep Resolution”
    set output to words of (do shell script command)
    set {dspwidth, dspheight} to {item 2, item 4} of output

  21. Doesn’t work for me. It says: “System Events got an error: Access for assistive devices is disabled.”, altought assistive devices is enabled in Universal Access.

    Thank You.

  22. A quick search turned up the following: http://stackoverflow.com/questions/10140334/applescript-system-events-error-access-for-assistive-devices-is-disabled

    It would seem the error isn’t correct. Applescript is returning this error because some application you have open is not able to properly reply. Most likely “Google Chrome Helper”, but it could be some other. Once you figure it out just add it to the list at the top of the script, something like so should take care of it:

    property processesToIgnore : {“Google Chrome Helper”}

Leave a Reply

Your email address will not be published.