Hello,
I’m trying to make an Applescript to tile my terminal windows, since Apple unfortunately doesn’t provide this functionality. In short, I want to mirror Ubuntu Linux functionality:
- Anywhere in the OS, at any time (okay, almost any time), I can press a keyboard shortcut to launch a new terminal window.
- This window should pop in the current space regardless of whether Terminal is open at all or if there are Terminal windows in the current or other spaces already.
- New Terminal windows should be tiled with respect to existing terminal windows on the current space only.
I’ve got a script that gets the tiling part down just based on a count of existing Terminal windows. However, there are a few things I’d like to do to improve the script, and I’m not sure if they’re possible.
- Count only the Terminal windows in the current space. Right now, the tiling is foobared if there are Terminal windows open in other spaces (which is usually the case for me).
- Launch the Terminal application, create a new window, and move it to the upper right corner of the screen for the first window. Right now, my script fails if there are no existing Terminal windows.
- Assign a keyboard shortcut to my launcher. I assume the best way to do this would be to use some third-party application? I don’t think it’s possible to execute an application using Apple’s custom keyboard shortcut options in the system preferences.
Any advice would be appreciated. Thanks very much in advance,
Adrian
property xArray : {0, 400, 800}
property yArray : {0, 250, 500}
tell application "Terminal"
activate
try
set numWin to the count (the windows of application "Terminal")
set modWin to numWin mod 9
set xIndex to (round (modWin / 3) rounding down) + 1
set yIndex to (modWin mod 3) + 1
set xPos to item xIndex of xArray
set yPos to item yIndex of yArray
if (numWin is not equal to 0) then
tell application "System Events"
tell process "Terminal"
keystroke "n" using command down
end tell
end tell
end if
tell front window
set position to {xPos, yPos}
end tell
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
end try
end tell