//go:build darwin package main // notify_darwin.go — M8-4 R5-D1:macOS OS 原生通知 // // 用 osascript 的 display notification 語法。使用者首次會看到系統通知授權對話框; // 若被拒絕,後續呼叫靜默失敗(這是預期行為,最佳努力送達)。 // // TDD ground truth: // - .autoflow/04-architecture/v2/server-lifecycle.md §10.2 import ( "fmt" "os" "os/exec" "strings" ) // sendCrashNotification 發一則 OS 原生通知。非阻塞、fire-and-forget。 // // 呼叫端應該用 `go sendCrashNotification(...)` 的形式呼叫。 // 本函式內任何錯誤只 log 到 stderr,不往上拋。 func sendCrashNotification(title, body string) { safeTitle := escapeAppleScript(title) safeBody := escapeAppleScript(body) script := fmt.Sprintf( `display notification "%s" with title "%s" sound name "Funk"`, safeBody, safeTitle, ) cmd := exec.Command("osascript", "-e", script) if err := cmd.Run(); err != nil { fmt.Fprintf(os.Stderr, "[notify] osascript failed: %v\n", err) } } // escapeAppleScript 對 AppleScript 字串做最小 escape。 // AppleScript 字串用雙引號包圍,需要 escape 反斜線和雙引號。 func escapeAppleScript(s string) string { s = strings.ReplaceAll(s, `\`, `\\`) s = strings.ReplaceAll(s, `"`, `\"`) return s }