Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os/exec"
- "strings"
- "time"
- )
- func main() {
- for {
- t, err := ActiveWindowTitle()
- if err != nil {
- panic(err)
- }
- fmt.Println(t)
- time.Sleep(1 * time.Second)
- }
- }
- func ActiveWindowTitle() (string, error) {
- // Grab the ID of the current active window
- out, err := exec.Command("xprop", "-root", "_NET_ACTIVE_WINDOW").Output()
- if err != nil {
- return "", err
- }
- parts := strings.Split(string(out), " ")
- if len(parts) != 5 {
- return "", fmt.Errorf("unexpected output from xprop -root _NET_ACTIVE_WINDOW: %s", string(out))
- }
- id := parts[4]
- // Grab the title of the current active window, using ID
- out, err = exec.Command("xprop", "-id", id, "WM_NAME").Output()
- if err != nil {
- return "", err
- }
- parts = strings.Split(string(out), "=")
- if len(parts) != 2 {
- return "", fmt.Errorf("unexpected output from xprop -id ID WM_NAME: %s", string(out))
- }
- return strings.Trim(strings.TrimSpace(parts[1]), `"`), nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement