Skip to content

Instantly share code, notes, and snippets.

@darwin
Created September 18, 2011 20:29
Show Gist options
  • Save darwin/1225516 to your computer and use it in GitHub Desktop.
Save darwin/1225516 to your computer and use it in GitHub Desktop.
def run_in_terminal(cmd, title, background=[0,0,0])
# 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
puts yellow("!> #{cmd}")
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
end
def find_terminal_tab(color)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment