Zenn
Closed7

スクリプトからiTerm2のタブ名を動的に変更したい

Gekitenius.YGekitenius.Y

開発環境で複数ディレクトリでNodeサーバーを立ち上げるとき、エディタ上でやってると別PJ開いたときに鯖が死んだりして面倒なのでiTerm2で立ち上げておきたい。
そしてiTerm2で立ち上げるときには、どの鯖がどのディレクトリのものか分かるようにしておきたい(ので、タブに名前を付けたい)
https://qiita.com/aidy91614/items/ebaad1009dcdf16401cf

Gekitenius.YGekitenius.Y
完成品
#!/bin/sh

# Base project directory
BASE_DIR="/Users/userName/yourDir"

# Function to start a service in a new iTerm tab
start_service() {
    dir=$1
    command=$2
    title=$3

    # Check if directory exists
    if [ ! -d "$dir" ]; then
        echo "Error: Directory $dir does not exist"
        return 1
    fi

    # Create new tab in iTerm and execute command
    osascript <<EOF
        tell application "iTerm"
            tell current window
                create tab with default profile
                tell current session
                    write text "echo \"=== $title ===\""
                    write text "cd \"$dir\""
                    write text "nameiterm \"$title\""
                    write text "$command"
                end tell
            end tell
        end tell
EOF
}

# Start all services
echo "Starting all XXXX services..."

# Create new iTerm window
osascript <<EOF
    tell application "iTerm"
        create window with default profile
        activate
    end tell
EOF

# Frontend dirA
start_service "$BASE_DIR/frontend/dirA" "npm run dev" "Front dirA"

# Frontend dirB
start_service "$BASE_DIR/frontend/dirB" "npm run dev" "Front dirB"

# API dirC
start_service "$BASE_DIR/api/dirC" "npm run start" "API dirC"

# API dirD
start_service "$BASE_DIR/api/dirD" "npm run start" "API dirD"

echo "All services have been started. Check individual tabs in iTerm window for status."
Gekitenius.YGekitenius.Y

立ち上げまでのスクリプトはClaudeが書いてくれた
iTerm2の名前設定する部分を追加実装しようとしたときに下記のエラーが出る。
write text "echo -ne "\e]1;$title\a""の部分がダメらしい
334:335: syntax error: “"”があるべきところですがunknown tokenが見つかりました。 (-2741)

#!/bin/sh

# Base project directory
BASE_DIR="/Users/name/yourDir"

# Function to start a service in a new iTerm tab
start_service() {
    dir=$1
    command=$2
    title=$(basename "$dir")

    # Check if directory exists
    if [ ! -d "$dir" ]; then
        echo "Error: Directory $dir does not exist"
        return 1
    fi

    # Create new tab in iTerm and execute command
    osascript <<EOF
        tell application "iTerm"
            tell current window
                create tab with default profile
                tell current session
                    write text "echo \"=== $title ===\""
                    write text "cd \"$dir\""
                    write text "echo -ne \"\e]1;$title\a\""
                end tell
            end tell
        end tell
EOF
}
Gekitenius.YGekitenius.Y

Claudeに聞いて色んな記述方法を試したがどれも上手く行かない(syntax errorを回避できない)ので、もとのQiitaの記事の後半に書いてあった方法を取ってみる。

Gekitenius.YGekitenius.Y

bashrcをzshrcに変えただけ、ほかは同じ
スクリプト内の1行目も変えるの忘れずに(忘れてた

$ mkdir ~/bin
$ echo 'export PATH=$PATH:$HOME/bin' >> ~/.zshrc
$ source ~/.zshrc
$ vim ~/bin/nameiterm
$ chmod +x ~/bin/nameiterm
$ cat ~/bin/nameiterm

#! /bin/zsh
echo -ne "\e]1;$1\a"
Gekitenius.YGekitenius.Y
  • 先に作ったカスタムコマンドを利用するように修正
  • 第三引数の名前を設定するように修正
start_service() {
    dir=$1
    command=$2
    title=$3

    # Check if directory exists
    if [ ! -d "$dir" ]; then
        echo "Error: Directory $dir does not exist"
        return 1
    fi

    # Create new tab in iTerm and execute command
    osascript <<EOF
        tell application "iTerm"
            tell current window
                create tab with default profile
                tell current session
                    write text "echo \"=== $title ===\""
                    write text "cd \"$dir\""
                    write text "nameiterm \"$title\""
                end tell
            end tell
        end tell
EOF
}
Gekitenius.YGekitenius.Y

動作することを確認
完成品は最初のコメントにぶら下げておいた

このスクラップは3ヶ月前にクローズされました
ログインするとコメントできます