Skip to content

Instantly share code, notes, and snippets.

@saltlakeryan
Last active March 1, 2023 19:40
Show Gist options
  • Save saltlakeryan/5a2df3163db168a05c3a108edfdd1d9f to your computer and use it in GitHub Desktop.
Save saltlakeryan/5a2df3163db168a05c3a108edfdd1d9f to your computer and use it in GitHub Desktop.
applescript to tile windows in a 4x4 grid
-- Inspirations: https://stackoverflow.com/questions/14551419/listing-all-windows-of-all-applications
-- 2: https://gist.github.com/krid78/4673164
-- 3: https://gist.github.com/dpinney/d7b83b1ac3405aee244547e42b14ce72
-- 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
-- Inspiration: https://stackoverflow.com/questions/14551419/listing-all-windows-of-all-applications
tell application "System Events"
set this_info to {}
set allProcs to (application processes where visible is true and background only is false)
-- 4x4 grid
set gridx to 4
set gridy to 4
set x to 0
set y to 0
set width to 3000
set height to 1760
set xdiff to (width / gridx)
set ydiff to (height / gridy)
set lrow to 0
set col to 0
set loopIndex to 0
repeat with theProcess in allProcs
try
set new_info to (value of (first attribute whose name is "AXWindows") of theProcess)
set this_info to this_info & new_info
repeat with my_window in (windows of theProcess)
set |currentPosition| to position of my_window
log "current position of " & (name of my_window) & " : " & (item 1 of |currentPosition|) & ", " & (item 2 of |currentPosition|)
if (item 1 of |currentPosition|) >= 0 then
set position of my_window to {x, y}
set size of my_window to {xdiff, ydiff}
set loopIndex to (loopIndex + 1)
set col to ((col + 1) mod gridx)
set lrow to round (loopIndex / gridx) rounding down
set lrow to (lrow mod gridy)
set x to (xdiff * col)
set y to (ydiff * lrow)
log "x: " & x
log "y: " & y
log "row: " & lrow
log "col: " & col
end if
end repeat
end try
end repeat
--this_info -- display list in results window of AppleScript Editor
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment