package ghostty import ( "bytes" "context" "errors" "fmt" "os/exec" "strings" "time" ) // The bundle id is used instead of the name so that the script never // launches a different app, and the "is running" check keeps it from // starting Ghostty when it is closed. const reloadScript = ` if application id "com.mitchellh.ghostty" is running then tell application id "com.mitchellh.ghostty" if (count of terminals) is 0 then return "no terminals" return perform action "reload_config" on first terminal end tell else return "not running" end if` func reload() error { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() cmd := exec.CommandContext(ctx, "osascript", "-e", reloadScript) var stdout, stderr bytes.Buffer cmd.Stdout, cmd.Stderr = &stdout, &stderr err := cmd.Run() out := strings.TrimSpace(stdout.String()) if err != nil { msg := strings.TrimSpace(stderr.String()) switch { case errors.Is(ctx.Err(), context.DeadlineExceeded): return errors.New("osascript timed out (is a permission dialog open?)") case strings.Contains(msg, "-1743"): return errors.New("not allowed to control Ghostty; allow it in System Settings → Privacy & Security → Automation") case strings.Contains(msg, "-1708"), strings.Contains(msg, "-10000"): return errors.New("Ghostty refused the AppleScript command (is macos-applescript = false set?)") case msg != "": return fmt.Errorf("osascript: %s", msg) } return fmt.Errorf("osascript: %w", err) } switch out { case "true": return nil case "not running": return ErrNotRunning case "no terminals": return errors.New("Ghostty has no open terminal to send the reload to") default: return fmt.Errorf("Ghostty did not perform reload_config (%s)", out) } }