// 截取正在运行的 MyGoland 窗口,用于本地像素级验证。 package main import ( "bufio" "encoding/json" "fmt" "net" "os" ) type windowInfo struct { ID int `json:"id"` Title string `json:"title"` } func main() { if len(os.Args) < 3 { fmt.Println("用法: shot ") os.Exit(2) } c, err := net.Dial("unix", os.Args[1]) if err != nil { fmt.Println("连接失败:", err) os.Exit(1) } defer c.Close() enc := json.NewEncoder(c) dec := json.NewDecoder(bufio.NewReader(c)) if err := enc.Encode(map[string]any{"id": 1, "method": "windows"}); err != nil { fmt.Println("发送失败:", err) os.Exit(1) } var wr struct { Result struct { Windows []windowInfo `json:"windows"` } `json:"result"` Error any `json:"error"` } if err := dec.Decode(&wr); err != nil { fmt.Println("解析失败:", err) os.Exit(1) } if len(wr.Result.Windows) == 0 { fmt.Println("没有窗口") os.Exit(1) } id := wr.Result.Windows[0].ID if err := enc.Encode(map[string]any{"id": 2, "method": "capture", "params": map[string]any{"window": id}}); err != nil { fmt.Println("发送失败:", err) os.Exit(1) } var cr struct { Result struct { PNG []byte `json:"png"` Width int `json:"width"` Height int `json:"height"` } `json:"result"` Error any `json:"error"` } if err := dec.Decode(&cr); err != nil { fmt.Println("解析失败:", err) os.Exit(1) } if len(cr.Result.PNG) == 0 { fmt.Printf("截图失败: %+v\n", cr.Error) os.Exit(1) } if err := os.WriteFile(os.Args[2], cr.Result.PNG, 0o644); err != nil { fmt.Println("写文件失败:", err) os.Exit(1) } fmt.Printf("ok %dx%d -> %s\n", cr.Result.Width, cr.Result.Height, os.Args[2]) }