tmux使えばいいんだけど、tmux使わずにiTerm2の画面分割でどうにかするとき。初めてAppleScriptで実用的なものを書いてみた。iTerm2のAppleScriptで全部できるかと思ったら、
によると画面分割はサポートされてないらしい。なのでSystem Eventsとかいうやつにkeystrokeを渡して分割してもらう。なんかこんな適当な感じで動くのがよい。
#やってること (あとで思い出せるように)
set t to (make new terminal)
で新しいターミナルを開いてt
という変数にする。
set number of columns to 100
set number of rows to 30
横100文字、縦30文字にする。(どうせあとでフルスクリーンにするので関係ない。)
set s to (launch session "Default session")
sessionを開いてs
に入れる。(sessionとterminalの違いがよくわからないけど動いたので気にしない。)
write text "ssh oka@host"
write text "bash"
つくったsessionにコマンドを入力する。ここではssh。ssh先のデフォルトshellがbashじゃないのでbashじゃないのでbash
も入力して起動する。(正確にはbashなんだけど~/.bashrc
を読み込んでくれていない状態なので自分で起動してる。ここでは特に関係ないけど。)
tell i term application "System Events" to keystroke "d" using {command down}
ここでiTerm2じゃなくてOSのアプリっぽい何かで無理やりコマンド押しながらd(iTerm2の画面分割のショートカット)を送信して横に分割。
tell i term application "System Events" to keystroke "ssh oka@host"
tell i term application "System Events" to keystroke return
tell i term application "System Events" to keystroke "bash"
tell i term application "System Events" to keystroke return
分割した先でもsshを始める。
tell i term application "System Events" to keystroke "d" using {shift down, command down}
今度はcmd+shift+d
で縦に分割。こんな感じで、左に画面1つでエディタを起動、右は2つに分割してmake
したり実行したりという使い方をいつもしてる。
tell i term application "System Events" to keystroke "[" using {command down}
tell i term application "System Events" to keystroke "[" using {command down}
2個前の画面に戻るためにiTerm2のショートカット(cmd+[)を2回送信する。
tell i term application "System Events" to keystroke return using {command down}
最後にフルスクリーンのショートカットを押して完成。このショートカット(cmd+return)は初めて知った。