Count windows in current space?

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:

  1. Anywhere in the OS, at any time (okay, almost any time), I can press a keyboard shortcut to launch a new terminal window.
  2. 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.
  3. 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.

  1. 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).
  2. 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.
  3. 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

Well, I managed to handle the cases of “Terminal.app open but no windows open” and “Terminal.app closed.” Now I only have to figure out how to count only the windows on the current desktop (if possible).


property xArray : {0, 534, 1068}
property yArray : {0, 415, 805}

tell application "System Events"
	if (get name of every application process) contains "Terminal" then
		set AlreadyOpen to true
	else
		set AlreadyOpen to false
	end if
end tell

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
		tell application "System Events"
			tell process "Terminal"
				keystroke "n" using command down
			end tell
		end tell
		if AlreadyOpen is false then
			set xPos to 0
			set yPos to 0
			close window 2
		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