Last active
June 26, 2023 07:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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