ハイパーマッスルエンジニア

Vim、ShellScriptについてよく書く

iTermをAppleScriptで操作する

f:id:rasukarusan:20190406224356p:plain:w500

基本的には以下の公式ドキュメントがわかりやすいし参考になる。

www.iterm2.com

ただ実際使う時どうなるの、どうすんの?っていうのを紹介する。

AppleScriptでiTermを操作する基本的な構文

iterm.sh

#!/usr/bin/osascript
tell application "iTerm"
    activate
    set _current_session to current session of current window
    tell _current_session
        # ここに操作したいことを書く
        set columns to 90 # ex.) カラム数を変更
    end tell
end tell

実行は以下

$ osascript iterm.sh

# もしくは実行権限を与えて
$ ./iterm.sh

f:id:rasukarusan:20190406215804g:plain
カラム数を変更

.scptで保存してosascript iterm-test.scptと実行してもいいが、.scptだとgitでpushした時にバイナリ文字列になってしまうので管理がし辛い。
なのでシェバンにosascriptと書いてしまうのが色々とやりやすいと思う。
パスは一応which osascriptで確認するのが吉ね。

透明度を変更

set transparency to 0.8

f:id:rasukarusan:20190406220145g:plain
透明度を変更

0~1を設定できて1に近づくほど透明度が上がる。

画面を分割

# 縦分割
split vertically with default profile

# 横分割
split horizontally with default profile

f:id:rasukarusan:20190406221116g:plain
画面分割

プロファイルが複数あるなら、プロファイル名を指定して分割することも可能。

split vertically with profile "プロファイル名"

# ex.) プロファイル名が"MyTestProfile"の場合
# split vertically with profile "MyTestProfile"

f:id:rasukarusan:20190406220243p:plain

f:id:rasukarusan:20190406220316g:plain
Profileを指定して画面分割

分割先にだけ適用できるから地味に便利。

画面分割後、分割先の画面でコマンド実行

tell application "iTerm"
    activate
    set _current_session to current session of current window

    # 画面を分割
    tell _current_session
        split vertically with default profile
    end tell

    # 分割先画面での実行
    tell last session of current tab of current window
        # 分割先のttyを取得
        set last_session_tty to tty as text
        do shell script "echo " & quoted form of last_session_tty
    end tell

end tell

f:id:rasukarusan:20190406222545g:plain
画面分割後、分割先でコマンド実行

画面分割後、フォーカスを分割後の画面に移したいときはselectを使う

...
tell last session of current tab of current window
    # フォーカスを分割後の画面に移す
    select
    # コマンドを実行する
    write text "echo hoge"
end tell

f:id:rasukarusan:20190406222800g:plain
画面分割後、分割先にフォーカルを移しコマンドを実行

ちなみにtell last session of current tab of current windowではなく

tell first session of current tab of current window
tell second session of current tab of current window
tell third session of current tab of current window
tell ....

のようにfirst, second, ...で指定することもできる。

バックグラウンドの色を変更

set background color to {red, green, blue, alpha}

f:id:rasukarusan:20190406220417g:plain
バックグラウンドの色を変更

各値には0~65536の値を入れる。(256ではないので注意) 例えば青っぽい色に変えたいならこんな感じ。

set background color to {8700, 400, 24200, 1}

適当に青っぽいRGB値に二桁0を足せば似たような色になる(上記は元の値は(87,4,242)

バックグラウンド画像を変更

set background image to "/Users/rasukarusan/output.png"

f:id:rasukarusan:20190406223741g:plain
バックグラウンド画像を変更

注意したいのは画像パスは絶対パスで書くこと。~で書くと別の意味で解釈されてしまうのでうまいこと設定できない。「POSIX Applescript path」とかで調べると変換方法とか出てくるので調べてみて。

元に戻すときは画像パスに空文字を指定してあげる

# 背景画像をもとに戻す
set background image to ""

現在のカラム数やttyを取得する

取得できる値は公式ドキュメントに色々書かれている。 こんな形で取得できるよという形を紹介しておく。

tell application "iTerm"
    activate
    set _current_session to current session of current window
    tell _current_session
        # カラム数を取得
        set current_column to columns
        do shell script "echo " & current_column
    end tell
end tell

f:id:rasukarusan:20190406215456g:plain
カラム数を取得

基本的にはset 変数名 to 取得したいもの(iTermで定められている変数名)で取得できる。 例えばttyを取得したいなら

set current_tty to tty

で取得できる。

終わり

他にも色々あるけどとりあえず使えそうなやつはこんなものだろうか。
特に画面分割やttyを取得できるのは色々と応用できそうなので、またそれも後日まとめる。

AppleScriptは単体ではなく、ShellScriptと組み合わせると幅が広がるので可能性は無限大だと思う。