Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@docwhat
Created September 8, 2016 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docwhat/a7bd4bbaf1f49e947c5bf2c1b8fa6561 to your computer and use it in GitHub Desktop.
Save docwhat/a7bd4bbaf1f49e947c5bf2c1b8fa6561 to your computer and use it in GitHub Desktop.
Gather Windows: useful when disconnecting a second monitor
-- Variation on
-- http://zach.in.tu-clausthal.de/software/Move%20Windows%20To%20Main%20Display.scpt.gz
--
-- Differences:
-- All windows that are more or less off-screen (no matter how much) are moved back
-- so that they are completely on screen (if possible).
-- Gabriel Zachmann, Jan 2008
-- List of processes to ignore (example {"xGestures", "OtherApp", ...})
property processesToIgnore : {"Typinator", "Google Chrome Helper"}
-- 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 menuH to 22 -- height of top menu bar
repeat with i from 1 to count allProcesses
-- display dialog (name of (process i)) as string
if not (processesToIgnore contains ((name of (process i)) as string)) 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
set winSize to size of window x
set _w to item 1 of winSize
set _h to item 2 of winSize
--display dialog (name as string) & " - width: " & (_w as string) & " height: " & (_h as string)
if (_x < 0 or _y < menuH or _x + _w > screen_width or _y + _h > screen_height) then
-- order does matter!
if (_x + _w > screen_width) then set _x to screen_width - _w
if (_x < 0) then set _x to 0
if (_y + _h > screen_height) then set _y to screen_height - _h
if (_y < menuH) then set _y to menuH
set position of window x to {_x, _y}
end if
end repeat
end tell
end if
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment