Skip to content

Instantly share code, notes, and snippets.

@joshualyon
Created June 20, 2025 15:41
Show Gist options
  • Save joshualyon/e846816b62fc939e562d24aaf2130f3e to your computer and use it in GitHub Desktop.
Save joshualyon/e846816b62fc939e562d24aaf2130f3e to your computer and use it in GitHub Desktop.
Open or foreground a dedicated 'claude' instance of Ghostty
#!/usr/bin/env bash
PROJECT_DIR="${1:-$(cd "$(dirname "$0")/.." && pwd)}"
APP_BUNDLE="/Applications/Ghostty.app"
WINDOW_TITLE="Claude WebStorm" # must match the --title you pass below
GHOSTTY_ARGS=(
--initial-command="claude /ide"
--title="$WINDOW_TITLE"
--working-directory="$PROJECT_DIR"
--macos-icon="retro"
--quit-after-last-window-closed=true
)
#--macos-icon=custom-style
#--macos-icon-ghost-color=BCBEC4
#--macos-icon-screen-color=FFFFFF
# Original Reference Command:
# open -na "/Applications/Ghostty.app" --args --working-directory="$ProjectFileDir$" --command="claude /ide" --title="Claude WebStorm" --macos-icon="retro" --quit-after-last-window-closed=true
# Get all Ghostty PIDs using ps (most comprehensive)
PIDS=($(ps aux | grep -i ghostty | grep -v grep | awk '{print $2}'))
# Check each PID for the target window title in command line
for pid in "${PIDS[@]}"; do
cmdline=$(ps -p "$pid" -o args= 2>/dev/null || echo "")
if [[ "$cmdline" == *"$WINDOW_TITLE"* ]]; then
# Found the process with our window title, bring it to front
osascript -e "tell application \"System Events\" to set frontmost of (first application process whose unix id is $pid) to true"
exit 0
fi
done
# Window not found, launch new instance
open -na "$APP_BUNDLE" --args "${GHOSTTY_ARGS[@]}"
# Original slow but working AppleScript approach (commented out for reference):
#osascript <<EOF
#tell application "System Events"
# set found to false
# repeat with p in application processes
# if name of p is "Ghostty" then
# repeat with w in windows of p
# if name of w is "$WINDOW_TITLE" then
# set frontmost of p to true
# set found to true
# exit repeat
# end if
# end repeat
# if found then exit repeat
# end if
# end repeat
#end tell
#
#if not found then
# do shell script "open -na '$APP_BUNDLE' --args ${GHOSTTY_ARGS[*]}"
#end if
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment