Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "strings"
  7. "time"
  8. )
  9.  
  10. func main() {
  11. for {
  12. t, err := ActiveWindowTitle()
  13. if err != nil {
  14. panic(err)
  15. }
  16. fmt.Println(t)
  17. time.Sleep(1 * time.Second)
  18. }
  19. }
  20.  
  21. func ActiveWindowTitle() (string, error) {
  22. // Grab the ID of the current active window
  23. out, err := exec.Command("xprop", "-root", "_NET_ACTIVE_WINDOW").Output()
  24. if err != nil {
  25. return "", err
  26. }
  27. parts := strings.Split(string(out), " ")
  28. if len(parts) != 5 {
  29. return "", fmt.Errorf("unexpected output from xprop -root _NET_ACTIVE_WINDOW: %s", string(out))
  30. }
  31. id := parts[4]
  32.  
  33. // Grab the title of the current active window, using ID
  34. out, err = exec.Command("xprop", "-id", id, "WM_NAME").Output()
  35. if err != nil {
  36. return "", err
  37. }
  38. parts = strings.Split(string(out), "=")
  39. if len(parts) != 2 {
  40. return "", fmt.Errorf("unexpected output from xprop -id ID WM_NAME: %s", string(out))
  41. }
  42. return strings.Trim(strings.TrimSpace(parts[1]), `"`), nil
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement