title function

1
2
3
4
function title () { 
  unset PROMPT_COMMAND # more on this later
  echo -ne "\e]0;$1\a"
}

set_window_and_tab_title

1
2
3
4
5
6
7
8
function set_window_and_tab_title {
  local title="$1"
  [[ -z "$title" ]] && title="root"
  echo -ne "\e]0;${title}\a"
}

PROMPT_COMMAND='set_window_and_tab_title "${PWD##*/}"'

RailsTerm AppleScript

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- RailsTerm 0.2
-- 

-- Settings --
set projects_root to "~/Projects" -- where you keep your projects
set presets to {"first_project", "another_project", "one_more_project"} -- sub directories to “project_root'”
set visor_enabled to true -- if you use Visor “http://docs.blacktree.com/visor/visor”, set this to true
set rails_server_command to "ss" -- My `ss` bash function will use the next available port (http://pastie.textmate.org/221395) so I can run multiple rails projects at once. if you don't have a `ss` alias/script, just use `./script/server` for mongrel/thin etc. If you're on mod_rails, just `tail -f log/development.log`

-- Run it
global rails_dir, min_window_count, presets, projects_root, visor_enabled, rails_server_command
main()

-- Main start function
on main()
  my set_minimum_window_count()
  
  tell application "Terminal"
    activate
    
    set rails_dir to my ask_for_rails_project_directory()
    
    if not my rails_dir_exists() then
      display alert ¬
        "Project is not a valid Rails directory" message ¬
        "The directory doesn't seem to contain some folders a Rails project usually do. Please check your path." as warning
    else
      my open_rails_tabs()
    end if
  end tell
end main

on set_minimum_window_count()
  if visor_enabled then
    set min_window_count to 2 -- Visor SIMBL seems to use 2 windows for itself
  else
    set min_window_count to 0
  end if
end set_minimum_window_count

on ask_for_rails_project_directory()
  activate
  
  set project_directory to ¬
    {choose from list presets ¬
      with title ¬
      "Choose Ruby on Rails project" OK button name ¬
      "Open Project" cancel button name ¬
      "Other Project..." default items ¬
      (first item of presets) with prompt "Choose any predefined project (in \"" & projects_root & "/\")"}
  
  -- If no project selected (i.e. clicked on Cancel), ask user to manually enter a project's path
  if project_directory = {false} then
    set project_directory to the text returned of (display dialog ¬
      "Please Enter the Path to Your Rails Directory" default answer ¬
      projects_root as text)
  else
    set project_directory to projects_root & "/" & project_directory
  end if
  
  return project_directory
end ask_for_rails_project_directory

on rails_dir_exists()
  set dir_exists to false
  
  try
    do shell script "cd " & rails_dir & " && [[ -x app ]] && [[ -x config ]] && echo exists"
    if text of result is "exists" then
      set dir_exists to true
    else
      set dir_exists to false
    end if
  on error
    set dir_exists to false
  end try
  
  return dir_exists as boolean
end rails_dir_exists

on open_rails_tabs()
  my open_tab("Server", rails_server_command)
  my open_tab("Console", "./script/console")
  my open_tab("Autotest", "autotest")
  my open_tab("Rails Directory", "mater") -- I use `mater` from http://pastie.textmate.org/221354 instead of `mate .`
end open_rails_tabs

on open_tab(title, the_command)
  tell application "Terminal" to activate
  
  my create_new_window_or_tabs()
  
  tell application "Terminal" to ¬
    do script with command ("cd " & rails_dir & "&& unset PROMPT_COMMAND && echo -n -e \"\\e]0;" & title & "\\a\" && " & the_command) ¬
      in last tab of window 1
end open_tab

on create_new_window_or_tabs()
  if (count of windows of application "Terminal") ≤ min_window_count then
    tell application "Terminal" to do script "" -- create a new window
  else
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
  end if
end create_new_window_or_tabs