darwin (owner)

Revisions

  • 48ad51 darwin Sat May 23 05:40:06 -0700 2009
gist: 116611 Download_button fork
public
Public Clone URL: git://gist.github.com/116611.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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