0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AppleScript スクリプト を使い Terminal / iTerm2 で コマンドを実行する

Last updated at Posted at 2025-10-19

・ Terminalでコマンドを実行する場合

osascript -e 'tell application "Terminal" to activate' \
	-e 'tell application "Terminal" to do script "echo \"It works\""'

しかし、TerminaliTerm2に変えただけでは、動きません。

% 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 を使用。

次の記事で紹介されている書き方だと 動きませんでした。おそらくバージョンの違いでしょう。




以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?