I had a brief run-in with AppleScript trying to overcome an OS X terminal bug.  and its not something I enjoyed.  I would much rather be able to do scripts like this in a language I'm more comfortable in like ruby.  I've known for a while that one of the enhancements in OS X Leopard is that it has native support for scripting the UI from within other scripting languages like python and ruby, however, I didn't realize that I could drop those scripts in the user scripts folder (~/Library/Scripts) and have them available from the scripts toolbar icon. Well, one can!  The trick to dong this is that the script has to be executable (chmod +x ~/Library/Scripts/Application/Terminal/Layout.rb), and needs to have a shebang to the desired  interpreter (#!/usr/bin/ruby as the first line.) Finally I can get rid of the only AppleScript code I was forced to write.  Here is a (better!) ruby equivalent of the AppleScript from the link above.


#!/usr/bin/ruby

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

yMin = 22
xMin = 187
xSize = 504
ySize = 365
xCount = 2

term = SBApplication.applicationWithBundleIdentifier_("com.apple.Terminal")
sorted_windows = term.windows.sort_by {|w| w.name.to_s.match(/([0-9]+)$/)[1] }
sorted_windows.each_with_index do |window, i|
newx = xMin + xSize * (i % xCount)
newy = yMin + ySize * (i / xCount).to_i
# Setting rect doesn't work, so set position instead even though it is
# deprecated
# rect = NSRect.new(newx, newy, xSize, ySize)
# window.bounds = rect
pos = NSPoint.new(newx, newy)
window.position = pos
# size doesn't work reliably
# size = NSPoint.new(xSize, ySize)
# window.size = size
end