・ Terminalでコマンドを実行する場合
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "Terminal" to do script "echo \"It works\""'
しかし、TerminalをiTerm2に変えただけでは、動きません。
% osascript -e 'tell application "iTerm2" to activate' \
-e 'tell application "iTerm2" to do script "echo \"It works\""'
70:76: syntax error: end of line などがあるべきところですが“script”が見つかりました。 (-2741)
・ iTerm2でコマンドを実行する場合
iTerm2で実行する場合は、次のように書きます。
osascript -e 'tell application "iTerm2" to activate' \
-e 'tell application "iTerm2" to tell current session of current window to write text "echo \"It works\""'
・ iTerm2で新しいタブを開いてコマンドを実行する場合
osascript -e 'tell application "iTerm2" to tell current window to create tab with default profile' \
-e 'tell application "iTerm2" to tell current session of current window to write text "echo \"New TAB\""'
当方(執筆時点で)は macOS 15.6.1 (24G90), iTerm2 3.6.2 を使用。
次の記事で紹介されている書き方だと 動きませんでした。おそらくバージョンの違いでしょう。
以上