Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to move all windows to the main display System
The following AppleScript will move all windows which are offscreen to the main screen. This is useful when you have disconnected an external display, and the windows you had open on that screen (or screens) are now unreachable.

Copy and paste this code into Script Editor:

-- Example list of processes to ignore: {"xGestures"} or {"xGestures", "OtherApp", ...}
property processesToIgnore : {}

-- Get the size of the Display(s), only useful if there is one display
-- otherwise it will grab the total size of both displays
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

The definitely works in 10.4, and it may also work on OS 10.3. You can find a link to the original source in this blog entry.

[robg adds: You'll need to have the Enable Access for Assistive Devices box checked in Universal Access. The above-linked blog entry contains info on how to exclude certain apps from the script -- for use with programs that hide windows offscreen. Also, if there are updates to the script, they'll be found there, not here. I have not tested this one.]
    •    
  • Currently 3.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (12 votes cast)
 
[35,166 views]  

An AppleScript to move all windows to the main display | 8 comments | Create New Account
Click here to return to the 'An AppleScript to move all windows to the main display' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to move all windows to the main display
Authored by: chabig on Oct 23, '07 08:03:59AM

I thought OS X correctly handled this situation. If not, you can always get the window back to the main screen by choosing Window > Zoom.



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: JonLaliberte on Oct 23, '07 06:42:38PM

This doesn't seem to work in all applications, it also requires doing it for each individual window.



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: jctull on Oct 23, '07 09:26:31AM

This is great for Qt-based applications that sometimes (always?) have a habit of not respecting display changes in OS X. I tested it on qgis, an open source GIS application that is problematic when I forget to move the main screen off the secondary display that then requires manual editing of its preference file, and it popped the window onto the main display with no problem. Kudos for this excellent and useful script!



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: mkoistinen on Oct 23, '07 10:09:34AM
I find this to be very useful when it is needed. I've added it to my Finder scripts listed in the Script Menu. I find that Firefox seems to have a funny 'hidden' window that appears when I use this. It looks like I need to hack the script to ignore this window for the Firefox process.

[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: billclinton on Oct 23, '07 02:22:20PM

Apple's window manager has a number of bugs (especially related to having two monitors in a vertical orientation as is common with laptops) but I don't think leaving windows unreachable is one of them. I'm curious to know under what circumstances this happens. Usually, such windows that would otherwise be offscreen get moved to the remaining connected screen at least partially.

I would swear that OS X has deteriorated in this respect. It seems to me that with OS 9, disconnecting a monitor would move all of the windows that were completely displayed on the disconnected monitor to the remaining monitor, and windows that were split stayed put. For the windows that were moved upon disconnecting a monitor, they resumed their previous places when the second monitor is reconnected.

That is all shot to hell with OS X--disconnect and reconnect a monitor and your windows are all over the place. At least with vertically-oriented monitors.



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: JonLaliberte on Oct 23, '07 06:48:00PM

It would be nice if this were the case. Unfortunately even applications such as DVD Player will not properly handle this situation. I believe if you leave the window on an external display and disconnect it after DVD player has quit, when DVD Player relaunched it will leave the window off screen still as well.

Indeed, OS X's handling of multiple displays leaves something to be desired.



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: pzyhlarz on Oct 25, '07 08:10:56AM

Would this script also work for a window from a program like Outlook 2001 for Mac in Classic emulation, when a program window was moved to another monitor and later the 2nd monitor was disconnected?

---
Austria does mosx too!



[ Reply to This | # ]
An AppleScript to move all windows to the main display
Authored by: sharik14 on Jan 21, '08 06:14:35AM

Thanks, this script is helpful!

This one is happening pretty consistently for me. I posted a bug to Apple .. here's the description I posted.

I have a Macbook Pro. I sometimes use an external monitor as the primary display (the one with the menu bar) without mirroring. When I connect the external monitor, windows move from the main screen to the secondary one (i.e., remain on the Macbook screen). I have to move them by hand.

Steps to Reproduce:
1. Connect an external monitor
2. Configure it in displays System Prefs so that
(a) mirroring is turned off
(b) external display has menu bar on it
[I have the arrangement set so that my macbook display is to the right of the external display]
3. Disconnect monitor. Open applications (e.g. iCal or Safari). Zoom their windows so that they're using a chunk of the screen
4. Reconnect the monitor.

Expected Results:
The windows of these applications remain on the primary (menubar) display, so that they're easily usable.
This is what happens with Tiger (and previous systems).

Actual Results:
With Leopard, the windows remain on the Macbook screen. This means I have to move the cursor offscreen, gather all my windows, and move them back to the main screen. It's driving me crazy!

Regression:
Problem only began with Leopard. In previous systems it worked fine.



[ Reply to This | # ]