Skip to content

Instantly share code, notes, and snippets.

@ndbroadbent
Last active September 22, 2022 11:46
Show Gist options
  • Save ndbroadbent/51f613da2ed643f514aca2e177739187 to your computer and use it in GitHub Desktop.
Save ndbroadbent/51f613da2ed643f514aca2e177739187 to your computer and use it in GitHub Desktop.
Using AppleScript to set up iTerm2 tabs and panes for Rails development
# iTerm2 AppleScript Docs: https://www.iterm2.com/documentation-scripting.html
on run argv
set current_dir to item 1 of argv
tell application "iTerm2"
tell current window
set rails_session to current session
set current_tab to current tab
tell current session
write text "bundle exec rails server --binding=127.0.0.1 --port 3000"
set sidekiq_session to split horizontally with default profile
tell sidekiq_session to select
write text "cd " & current_dir
write text "bundle exec sidekiq"
set client_session to split vertically with default profile
tell client_session to select
write text "cd " & current_dir & "/client"
write text "yarn run build:development"
end tell
# Only open some more tabs if this is a new window
if count of tabs is 1 then
set new_tab to create tab with default profile
tell current session of new_tab
write text "cd " & current_dir
write text "rails console"
end tell
# Open some extra tabs for git, rspec, deploys, etc.
repeat 3 times
set new_tab to create tab with default profile
tell current session of new_tab to write text "cd " & current_dir
end repeat
end if
tell current_tab to select
tell rails_session to select
end tell
end tell
end run

Documentation for iTerm2 AppleScript: https://www.iterm2.com/documentation-scripting.html


I have this alias in my ~/.bashrc:

alias ds='osascript .dev.scpt "$(pwd)"'

I set up a .dev.scpt script for each of my projects. These scripts set up my preferred panes and tabs in iTerm2, and start the processes for Rails development. The advantage of using "write text" is that you can can press Ctrl+C, "up", "enter" to restart any of the processes (like a normal bash session.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment