// 列出正在运行的 MyGoland 窗口的无障碍元素树,用于精确定位控件坐标。 // // 有了它就不用再靠肉眼估坐标:按 Label 一查就知道按钮在哪。 // 用法: tree [筛选关键词] package main import ( "bufio" "encoding/json" "fmt" "net" "os" "strings" ) type node struct { ID any `json:"id"` Role string `json:"role"` Label string `json:"label"` Value string `json:"value"` Bounds rect `json:"bounds"` Actions []string `json:"actions"` } type rect struct { X float32 `json:"x"` Y float32 `json:"y"` W float32 `json:"w"` H float32 `json:"h"` } func (r rect) center() (float32, float32) { return r.X + r.W/2, r.Y + r.H/2 } func main() { if len(os.Args) < 2 { fmt.Println("用法: tree [关键词]") os.Exit(2) } filter := "" if len(os.Args) >= 3 { filter = strings.ToLower(os.Args[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": "tree"}); err != nil { fmt.Println("发送失败:", err) os.Exit(1) } var res struct { Result struct { Nodes []node `json:"nodes"` } `json:"result"` Error any `json:"error"` } if err := dec.Decode(&res); err != nil { fmt.Println("读取失败:", err) os.Exit(1) } for _, n := range res.Result.Nodes { text := n.Label if text == "" { text = n.Value } if filter != "" && !strings.Contains(strings.ToLower(text), filter) { continue } cx, cy := n.Bounds.center() fmt.Printf("%-10s %-14s %6.0f,%-6.0f %4.0fx%-4.0f %s\n", n.Role, strings.Join(n.Actions, ","), cx, cy, n.Bounds.W, n.Bounds.H, text) } }