at backyard

Color my life with the chaos of trouble.

macOSのターミナルから現在のプロジェクトを選択した状態でCodexアプリを開く方法

macOSのターミナルからCodexアプリを開くときに、今いるプロジェクトフォルダを選択した状態で新規スレッドを開始したくて小さな関数を作った。

この関数を使うと、現在のディレクトリをCodexのworkspaceとして開ける。引数で別のディレクトリを渡すことも可能。

macOSにCodexアプリがインストールされている前提。また、コマンド内部の python3 はローカルパスを安全に URL エンコードするためだけに使っている。これもmacOSではじめから使えるコマンドなので採用している次第。

以下の関数を .zshrc などに書いて source ~/.zshrc を実施して使う想定です。

# Open Codex app with the current directory as a new thread workspace.
# Usage:
#   cdxnew
#   cdxnew .
#   cdxnew ~/your/project
#   cdxnew ../other-project
cdxnew() {
  local target="${1:-$PWD}"

  if [ ! -d "$target" ]; then
    echo "cdxnew: directory not found: $target" >&2
    return 1
  fi

  local abs_path
  abs_path="$(cd "$target" && pwd)"

  local encoded
  encoded="$(CODEX_PATH="$abs_path" python3 -c 'import os, urllib.parse; print(urllib.parse.quote(os.environ["CODEX_PATH"]))')"

  open "codex://new?path=$encoded"
}