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
| オブジェクト | 主要プロパティ | 主要要素 |
|---|---|---|
application | name、frontmost、front window、version | windows、terminals |
window | id、name、selected tab | tabs、terminals |
tab | id、name、index、selected、focused terminal | terminals |
terminal | id、name、working directory | None |
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 の場合、アクション文字列はキーバインド設定で使う
アクション名と一致します。参照:
キーバインドアクションリファレンス。
new surface configuration は再利用可能な値のレコードを作成し、
new window、new tab、split に
渡せます。
サポートされるフィールド:
font sizeinitial working directorycommandinitial inputwait after commandenvironment 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
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