Skip to content

Instantly share code, notes, and snippets.

@oiva
Last active June 26, 2023 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oiva/d4be4c98e8fb3d2fb482e8248f5e5465 to your computer and use it in GitHub Desktop.
Save oiva/d4be4c98e8fb3d2fb482e8248f5e5465 to your computer and use it in GitHub Desktop.
AppleScript to position newly created iTerm windows next to previous windows. Either in column or row direction
use framework "AppKit"
property ca : current application
on run {input, parameters}
-- calculate screen size
set activeScreenVisibleFrame to ca's NSScreen's mainScreen()'s visibleFrame()
set activeScreenSize to {ca's NSWidth(activeScreenVisibleFrame) as integer, ca's NSHeight(activeScreenVisibleFrame) as integer}
set screenWidth to item 1 of activeScreenSize
set screenHeight to item 2 of activeScreenSize
-- requested window size
set WindowWidth to 800
set WindowHeight to 500
set winWidth to WindowWidth
set winHeight to WindowHeight
-- fit at least 2x2 windows on screen
if (screenWidth / 2 < WindowWidth) then set winWidth to (screenWidth / 2) as integer
if (screenHeight / 2 < WindowHeight) then set winHeight to (screenHeight / 2) as integer
set direction to "column" -- "row" / "column"
tell application "iTerm"
set winCount to (count windows)
-- calculate the maximum number of columns and rows that can fit on the screen
set maxCols to (screenWidth div winWidth)
set maxRows to (screenHeight div winHeight)
set numberOfPositionedWindows to 0
repeat with i from 1 to winCount
-- loop windows backwards, 1st window is the latest
set thisWin to item (winCount - i + 1) of windows
-- see if other windows are positioned automatically by checking if their width and height match the default
set {x, y, x2, y2} to bounds of thisWin
set w to x2 - x
set h to y2 - y
if w is winWidth and h is winHeight then
set numberOfPositionedWindows to numberOfPositionedWindows + 1
end if
-- all windows positioned
if numberOfPositionedWindows = winCount then return
-- position the last window
if i is winCount then
if direction = "row" then
set column to numberOfPositionedWindows mod maxCols
set colPos to winWidth * column
set windowsOnLastRow to (numberOfPositionedWindows mod maxCols)
set row to (numberOfPositionedWindows - windowsOnLastRow) div maxCols
set rowPos to row * winHeight
else if direction = "column" then
set row to numberOfPositionedWindows mod maxRows
set rowPos to winHeight * row
set windowsOnLastColumn to (numberOfPositionedWindows mod maxRows)
set col to (numberOfPositionedWindows - windowsOnLastColumn) div maxRows
set colPos to col * winWidth
end if
set bounds of thisWin to {colPos, rowPos, colPos + winWidth, rowPos + winHeight}
end if
end repeat
end tell
return input
end run
@oiva
Copy link
Author

oiva commented Apr 20, 2023

I set this to run in my ~/.zshrc
osascript ./apple\ scripts/position-terminals.scpt

It's a bit slow (0.3 seconds) but the terminal is interactive while the script positions it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment