CODING WITH CUTLERY

It's dangeresque.

Shortening Daily Boot Time With iTerm2 and AppleScript

| Comments

Last week, it struck me that I spend a significant amount of time at the beginning of each day bootstrapping my work environment. I usually launch iTerm2 (my favorite Mac OS X Terminal replacement), type several commands, open some TextMate projects, start some Rails servers and on and on. Sounds like a job for automation!

Fortunately, iTerm2 comes with fairly good AppleScript support right out of the box. You can open new terminal windows and tabs, set properties on each and issue commands, among other things. Though it’s not perfect (for instance, you can’t yet easily open split panes), it’s enough to get one started down the road to workday automation nirvana.

Without further ado, here’s my current work environment script:

start_workday.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
tell application "iTerm"
	activate
	
	set myterm to (make new terminal)
	tell myterm
		set number of columns to 175
		set number of rows to 50
		
		-- Open first project tab.
		set hm_session to (launch session "Open to HM project")
		delay 5
		tell hm_session
			write text "mate ."
			write text "rails s"
		end tell
		
		-- Open second project tab.
		set hm_old_session to (launch session "Open to HM project (old)")
		delay 5
		tell hm_old_session
			write text "mate ."
			write text "rails s --port 3001"
		end tell
		
		-- Open tab for blog (I use Octopress).
		set blog_session to (launch session "Open to blog")
		delay 5
		tell blog_session
			write text "mate ."
			write text "rake watch"
		end tell
		
		-- Open a vertical pane on each tab, ending up back on the first.
		tell i term application "iTerm" to activate
		tell i term application "System Events"
		  key code 19 using command down
		  keystroke "d" using command down
		  key code 20 using command down
		  keystroke "d" using command down
		  key code 18 using command down
		  keystroke "d" using command down
		end tell
	end tell
end tell

For more AppleScript + iTerm2 goodness, check out this post by Samantha Halfon.

Also, I was able to find a decent AppleScript key code reference.

Happy automating!

EDIT: Altentee has written a helpful article on this subject with another sample work environment script.

Comments