#!/usr/bin/env ruby
OSX = PLATFORM =~ /darwin/
WIN = PLATFORM =~ /win32/
NIX = !(OSX || WIN)
require "rubygems"
begin
require 'appscript' if OSX
include Appscript if OSX
rescue LoadError
raise 'You must "sudo gem install rb-appscript"'
end
def run_in_terminal(cmd, title, background=[0,0,0])
if OSX
# see http://www.nach-vorne.de/2007/11/22/terminal-trick
# and http://onrails.org/articles/2007/11/28/scripting-the-leopard-terminal
# and http://blog.cbciweb.com/articles/2008/05/02/scripting-mac-terminal-using-ruby
begin
term = app('Terminal')
term.activate()
current_window = term.windows.first
old_tab = current_window.selected_tab.get()
tab = find_terminal_tab(background)
unless tab
app("System Events").application_processes["Terminal.app"].keystroke("t", :using => :command_down)
tab = current_window.tabs.last
end
term.do_script(cmd, :in => tab)
tab.background_color.set(background)
tab.title_displays_custom_title.set(true)
tab.custom_title.set(title)
current_window.selected_tab.set(old_tab)
sleep 0.1
rescue
die "OSX command failed"
end
else
# toto je min pohodlne, spustim server v aktualni konzoli
system(cmd)
end
end
def find_terminal_tab(color)
return unless OSX # umime zatim jen pro OSX
term = app('Terminal')
term.windows.get.each do |win|
begin
win.tabs.get.each do |tab|
return tab if color == tab.background_color.get()
end
rescue
end
end
nil
end
run_in_terminal("cd ~", "TabNameHere", [0,10000,0]) # slightly green background