After updating to lion, I needed to tweak the script I had written here, so here is the updated version with some more comments to help people modify and write their own Ruby/OSX scripts


#!/usr/bin/ruby
# -*- coding: utf-8 -*-

# Put this in Library/Scripts/Applications/Terminal
#
# To get the script to show up in menubar,
# run "AppleScript Editor" -> Preferences -> "Show script menu in menu bar"
#
# To figure out what properties are available, "AppleScript Editor" -> File -> "Open Dictionary"

require 'osx/cocoa'
include OSX
OSX.require_framework 'ScriptingBridge'

def alert(title, message='', is_error=false)
puts "Displaying #{is_error ? 'error ' : ''}dialog - #{title}: #{message}"
if is_error
NSRunCriticalAlertPanel(title, message, 'OK', nil, nil)
else
NSRunAlertPanel(title, message, 'OK', nil, nil)
end
end

rowSize = 24
colSize = 80
yMin = 22
xMin = 187
xCount = 2

# To get bundle ID, View Package Contents on App -> Contents/Info.plist -> Bundle ID
term = SBApplication.applicationWithBundleIdentifier_("com.apple.Terminal")

# set the # of rows and columns (global setting, so set to one tab, sets for all)
# term.windows.first.tabs.first.currentSettings.numberOfRows = rowSize
# term.windows.first.tabs.first.currentSettings.numberOfColumns = colSize
term.defaultSettings.numberOfRows = rowSize
term.defaultSettings.numberOfColumns = colSize

# sort on title to order according to ⌘N window selection shortcuts, index is some other order
sorted_windows = term.windows.sort_by {|w| w.name.to_s.match(/([0-9]+)$/)[1] }
sorted_windows.each_with_index do |window, i|
# puts "#{window.index}: #{window.name}"

# sizes should be the same for all windows after setting numberOfRows/Cols
xSize = window.bounds.width
ySize = window.bounds.height

newx = xMin + xSize * (i % xCount)
newy = yMin + ySize * (i / xCount).to_i

rect = NSRect.new(newx, newy, xSize, ySize)
window.bounds = rect

# If setting rect doesn't work, set position instead even though it is
# deprecated
# pos = NSPoint.new(newx, newy)
# window.position = pos
#
# size also deprecated
# size = NSPoint.new(xSize, ySize)
# window.size = size
end