Skip to content

Instantly share code, notes, and snippets.

@philwebster
Created April 16, 2016 00:12
Show Gist options
  • Save philwebster/dfdae93adb7a9a75c4ea884613870e38 to your computer and use it in GitHub Desktop.
Save philwebster/dfdae93adb7a9a75c4ea884613870e38 to your computer and use it in GitHub Desktop.
Applescript to move and resize windows so they are just on-screen. Need to grant assistive access to Script Editor in System Preferences to run.
tell application "Finder"
set _bounds to bounds of window of desktop
set _screenWidth to item 3 of _bounds
set _screenHeight to item 4 of _bounds
tell application "System Events"
repeat with theProcess in processes
if not background only of theProcess then
set theWindows to windows of theProcess
repeat with w in theWindows
set _position to position of w
set _size to size of w
set _x to item 1 of _position
set _y to item 2 of _position
set _width to item 1 of _size
set _height to item 2 of _size
if _width is greater than _screenWidth then
set _width to _screenWidth
end if
if _height is greater than _screenHeight then
set _height to _screenHeight
end if
if _x is less than 0 then
set _x to 0
end if
if _x + _width is greater than _screenWidth then
set _x to _screenWidth - _width
end if
if _y + _height is greater than _screenHeight then
set _y to _screenHeight - _height
end if
if not _position is equal to {_x, _y} or not _size is equal to {_width, _height} then
set the position of w to {_x, _y}
set the size of w to {_width, _height}
end if
end repeat
end if
end repeat
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment