Here you go
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "System Events"
set myApplication to name of first application process whose frontmost is true
end tell
set screenWidth to 1680 --2560 --1920
set screenHeight to 1440
tell application myApplication
set myWindowsCount to count of windows
if myWindowsCount = 0 then
display dialog "You don't have any windows open!" buttons "OK" default button 1
else if myWindowsCount ≤ 2 then
set myWidth to screenWidth / 2
set bounds of window 1 to {0, 0, myWidth, screenHeight}
try
set bounds of window 2 to {myWidth, 0, myWidth * 2, screenHeight}
end try
else if myWindowsCount ≥ 3 and myWindowsCount ≤ 4 then
set myWidth to screenWidth / 2
set myHeight to (screenHeight - 22) / 2
set bounds of window 1 to {0, 0, myWidth, myHeight}
set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
set bounds of window 3 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
try
set bounds of window 4 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
end try
else if myWindowsCount ≥ 5 and myWindowsCount ≤ 6 then
set myWidth to screenWidth / 3
set myHeight to (screenHeight - 22) / 2
set bounds of window 1 to {0, 0, myWidth, myHeight}
set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
set bounds of window 3 to {myWidth * 2, 0, myWidth * 3, myHeight}
set bounds of window 4 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
set bounds of window 5 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
try
set bounds of window 6 to {myWidth * 2, myHeight + 22, myWidth * 3, (myHeight * 2) + 22}
end try
else if myWindowsCount ≥ 7 and myWindowsCount ≤ 8 then
set myWidth to screenWidth / 4
set myHeight to (screenHeight - 22) / 2
set bounds of window 1 to {0, 0, myWidth, myHeight}
set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
set bounds of window 3 to {myWidth * 2, 0, myWidth * 3, myHeight}
set bounds of window 4 to {myWidth * 3, 0, myWidth * 4, myHeight}
set bounds of window 5 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
set bounds of window 6 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
set bounds of window 7 to {myWidth * 2, myHeight + 22, myWidth * 3, (myHeight * 2) + 22}
try
set bounds of window 8 to {myWidth * 3, myHeight + 22, myWidth * 4, (myHeight * 2) + 22}
end try
end if
end tell
Like I said, it is only working on the main monitor, meaning if you have put windows on a second monitor, they will be counted and placed on the main monitor. If anyone has a solution for that, it’d be great. I’ve tried to get the bounds of the windows, and if the first item is less than 0 to not count it but I can’t figure out the syntax for it.
I should also mention that I am NOT a programmer so there surely a better way to do this.