Skip to content

Instantly share code, notes, and snippets.

@pn11
Last active October 20, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pn11/864703b55f8ae7262527 to your computer and use it in GitHub Desktop.
Save pn11/864703b55f8ae7262527 to your computer and use it in GitHub Desktop.
An AppleScript for opening iTerm2 in fullscreen and splitting windows (and starting SSH session). iTerm2で画面分割してフルスクリーンにしつつSSH sessionを始めるスクリプト。
tell application "iTerm"
activate
set t to (make new terminal)
tell t
set number of columns to 100
set number of rows to 30
set s to (launch session "Default session")
tell s
write text "ssh oka@host"
write text "bash"
tell i term application "System Events" to keystroke "d" using {command down}
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
tell i term application "System Events" to keystroke "d" using {shift down, command down}
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
tell i term application "System Events" to keystroke "[" using {command down}
tell i term application "System Events" to keystroke "[" using {command down}
tell i term application "System Events" to keystroke return using {command down}
end tell
end tell
end tell

iTerm2で画面分割してフルスクリーンにしつつSSH sessionを始めるスクリプト

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)は初めて知った。

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