Automatically reposition Terminal windows based on their titles?

I’m pretty sure that what I want to do is impossible, but hope springs eternal, and I thought that I would ask, just in case.

In Snow Leopard (10.6.2), I’d like to use AppleScript to reposition existing Terminal windows based on their titles. In other words, I’d like to query all the existing Terminal windows and get each window’s title (I think I know how to do this much). Then, I’d like to reposition each of these Terminal windows to a title-specific location on the screen.

If this is even possible, could someone give me some hints on how it could be done?

Thanks in advance.

Something like this:

tell application "Terminal"
	set valwins to name of every window
	repeat with valwin in valwins
		if (valwin as string) contains "Terminal ” bash" then
			set bounds of window (valwin as string) to {200, 600, 700, 800}
		end if
		--return bounds of window 1
	end repeat
end tell

But, big but, the bounds are messed up, if you uncomment return bounds, you always get a different values for the Ys (item 2 and 4) while the X values remain accurate, (1 and 3). This one got {200, 400, 700, 600}. I cannot find a pattern, loop, addition, multiplication, it seems completely random.

Are you using multiple displays?

I get some odd results when I try to move something between my displays (“primary” == external display, has the menu bar; “secondary” == my iBook’s built-in display; the primary is arranged to the left of the built-in; the logical top of built-in display is configured to be a bit below the logical top of the external display (System Events shows it to be 55 pixels lower; this is to mimic the physical arrangement)).

When moving a window from “primary” to “secondary”, the window is positioned too physically high (numerically low) by 256. When moving from “secondary” to “primary”, it is too physically low (numerically high) by 256. But if the window is already on the ˜correct’ display, it is repositioned OK.

I used this code to test: (comment out the set line to get a position value)

tell application "Terminal"
	set valwins to name of every window
	repeat with valwin in valwins
		if valwin contains "ttyp2" then
			set a to bounds of window valwin
			set bounds of window valwin to {1558, 1, 2061, 393}
			return {a, bounds of window valwin}
		end if
	end repeat
end tell

I have not previously played with moving windows by scripting Terminal. I have some other code that I use regularly that scripts System Events to reposition windows from Mail, Terminal, and Activity Monitor. You might try something like this instead:

tell application "System Events"
	repeat with win in windows of application process "Terminal"
		if title of win contains "ttyp2" then
			set a to {position, size} of win
			set position of win to {1558, 257}
			set size of win to {503, 392}
			return {a, {position, size} of win}
		end if
	end repeat
end tell

The coordinates in the two scripts actually put the window in the same place on my “secondary” display. The Terminal based values for the “secondary” display are 256 lower than the System Events based values (the System Events ones seem saner since the location is neither at the top of the “secondary” display, nor (correspondingly) where the top of the “primary” display would be if it was logically extended over to the “secondary” display). For locations on the “primary” display, the effective coordinates are the same in both applications.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.4 (4531.21.10, r51280)
Operating System: Mac OS X (10.4)

Just an iMac.

I only have one screen; I’m using a MacBook Pro. I was trying this by scripting Terminal, and I got the same results when using the method which was originally suggested by Richard. That’s what prompted this query.

However, I didn’t think of trying System Events. This seems to work a lot better.

Thanks to both of you!