AppleScript (macOS)

AppleScript で macOS 上の Ghostty を自動化します。このガイドでは オブジェクトモデル、利用できるコマンド、実践的な例を扱います。

概要

macOS の Ghostty はネイティブの AppleScript 辞書を公開して おり、スクリプトからターミナルウィンドウ、タブ、分割ペインの 照会と操作ができます。これにより Ghostty を osascript のような ツール、ランチャーのワークフロー、エディタープラグイン、独自の 自動化スクリプトと簡単に統合できます。

AppleScript サポートは Ghostty 1.3.0 で導入されました。

スクリプティング辞書

AppleScript API の正確な情報源は Ghostty スクリプティング 定義ファイル:Ghostty.sdef です。これを閲覧する最良の方法は、Script Editor に 読み込み、macOS GUI で API を ブラウズすることです。

インストール済みのアプリバンドルから辞書を調べるには:

sdef /Applications/Ghostty.app | less

スクリプトが機能することを確認するには:

osascript -e 'tell application "Ghostty" to get version'

オブジェクトモデル

Ghostty の AppleScript モデルは階層的です:

application -> windows -> tabs -> terminals
オブジェクト主要プロパティ主要要素
applicationname、frontmost、front window、versionwindows、terminals
windowid、name、selected tabtabs、terminals
tabid、name、index、selected、focused terminalterminals
terminalid、name、working directoryNone

front window は最前面の Ghostty ウィンドウを返します。focused terminal はタブ内のアクティブな端末を返し、現在 フォーカスを持つ端末を操作する スクリプトに便利です。

オブジェクト照会の例

tell application "Ghostty"
    set win to front window
    set tab1 to selected tab of win
    set term1 to focused terminal of tab1
    set allTermsInWin to terminals of win

    set cwdMatches to every terminal whose working directory contains "ghostty"
end tell

これら 2 つのプロパティにより、アクティブなコンテキストでの スクリプトが簡潔になります:

tell application "Ghostty"
    set term to focused terminal of selected tab of front window
    input text "pwd\n" to term
end tell
tell application "Ghostty"
    set currentTerm to focused terminal of selected tab of front window
    set newTerm to split currentTerm direction right
    input text "echo split-ready\n" to newTerm
end tell

コマンド

作成とレイアウト

コマンド目的例の構文
new surface configuration再利用できる surface 設定レコードを作成する。set cfg to new surface configuration
new window新しいウィンドウを作成する。set win to new window with configuration cfg
new tab任意の対象ウィンドウに新しいタブを作成する。set t to new tab in win with configuration cfg
split端末を分割し、新しい端末を返す。set t2 to split t1 direction right with configuration cfg

split direction の値は right、left、down、 up です。

フォーカス、選択、ライフサイクル

コマンド目的例の構文
focus端末にフォーカスし、そのウィンドウを最前面に移動する。focus t1
activate windowウィンドウを最前面に移動する。activate window (window 1)
select tabタブを選択する。select tab (tab 2 of window 1)
close端末を閉じる。close (terminal 2 of selected tab of window 1)
close tabタブを閉じる。close tab (tab 2 of window 1)
close windowウィンドウを閉じる。close window (window 1)

入力とアクション

コマンド目的例の構文
input text端末にペースト形式の
テキスト入力を送る。input text "echo hello" to t1
send key修飾キー付きでキーの押下/リリース
イベントを送る。send key "enter" to t1
send mouse buttonマウスボタンイベントを送る。send mouse button left button to t1
send mouse positionポインター位置の更新を送る。send mouse position x 240 y 120 to t1
send mouse scroll精度/慣性オプション付きで
スクロールイベントを送る。send mouse scroll x 0 y -8 precision true to t1
perform action端末上で Ghostty アクション文字列を
実行する。perform action "toggle_fullscreen" on t1

send key と send mouse button の場合、action は press か release の どちらか を指定できます。modifiers はいずれかをコンマ区切りで 含む文字列です: shift、control、option、command。

perform action の場合、アクション文字列はキーバインド設定で使う アクション名と一致します。参照: キーバインドアクションリファレンス。

Surface 設定レコード

new surface configuration は再利用可能な値のレコードを作成し、 new window、new tab、split に 渡せます。

サポートされるフィールド:

  • font size
  • initial working directory
  • command
  • initial input
  • wait after command
  • environment variables(KEY=VALUE 文字列のリスト)

例:

tell application "Ghostty"
    set cfg to new surface configuration
    set initial working directory of cfg to POSIX path of (path to home folder) & "src/ghostty"
    set font size of cfg to 13
    set environment variables of cfg to {"EDITOR=nvim", "FZF_DEFAULT_OPTS=--height 40%"}

    set win to new window with configuration cfg
end tell

セキュリティ

AppleScript サポートは macOS 上で既定有効です。 macOS はオートメーション権限(TCC)でアプリ間オートメーションを 保護するため、他のアプリが Ghostty を操作する前に システムが確認を促します。

AppleScript サポートを完全に無効にしたい場合は: 次を 設定します:

macos-applescript = false

例

Tmux のようなレイアウトを構築する

set projectDir to POSIX path of (path to home folder) & "src/ghostty"

tell application "Ghostty"
    activate

    set cfg to new surface configuration
    set initial working directory of cfg to projectDir

    set win to new window with configuration cfg
    set paneEditor to terminal 1 of selected tab of win
    set paneBuild to split paneEditor direction right with configuration cfg
    set paneGit to split paneEditor direction down with configuration cfg
    set paneLogs to split paneBuild direction down with configuration cfg

    input text "nvim ." to paneEditor
    send key "enter" to paneEditor

    input text "zig build -Demit-macos-app=false" to paneBuild
    send key "enter" to paneBuild

    input text "git status -sb" to paneGit
    send key "enter" to paneGit

    input text "tail -f /tmp/dev.log" to paneLogs
    send key "enter" to paneLogs

    focus paneEditor
end tell

すべての端末にコマンドをブロードキャストする

set cmd to "date"

tell application "Ghostty"
    set allTerms to terminals

    repeat with t in allTerms
        input text cmd to t
        send key "enter" to t
    end repeat

    display dialog ("Broadcasted to " & (count of allTerms) & " terminal(s).")
end tell

タブタイトルを設定する

perform action と set_tab_title を使えば、スクリプトから タブにタイトルを付けられます。

新しいタブを作成しタイトルを設定する例:

tell application "Ghostty"
    set win to front window
    set cfg to new surface configuration

    set t1 to new tab in win with configuration cfg
    perform action "set_tab_title:Editor" on focused terminal of t1
    input text "nvim .\n" to focused terminal of t1

end tell

作業ディレクトリで端末にジャンプする

set needle to "ghostty"

tell application "Ghostty"
    set matches to every terminal whose working directory contains needle

    if (count of matches) = 0 then
        set matches to every terminal whose name contains needle
    end if

    if (count of matches) > 0 then
        set t to item 1 of matches
        focus t
        input text "echo '[focused by AppleScript]'" to t
        send key "enter" to t
    end if
end tell