forked from osuripple/ppwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowtitle_windows.go
executable file
·50 lines (40 loc) · 1.27 KB
/
windowtitle_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// +build windows
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"os/exec"
"strings"
)
const getWindowTitlePowershellExpression = `gps |
? {$_.mainwindowtitle -like "*%s*"} |
? {$_.processname -like "%s"} |
select mainwindowtitle`
func (tc *WindowTitleChecker) check() (bool, error) {
expr := fmt.Sprintf(getWindowTitlePowershellExpression, tc.PartialWindowTitle, tc.ProcessName)
log.Debugf("executing powershell expression: %s", strings.Replace(expr, "\n", " ", -1))
cmd := exec.Command("powershell", "-command", expr)
out, err := cmd.CombinedOutput()
if err != nil {
return false, err
}
// Strip all leading/trailing whitespace, separate into lines, delete empty lines
var outlines []string
for _, s := range strings.Split(strings.Trim(string(out), " "), "\r\n") {
if s != "" {
outlines = append(outlines, s)
}
}
log.Debugf("got %d lines from powershell", len(outlines))
// Check that we have content in the array, if not the window doesn't exist
if len(outlines) < 1 {
return false, fmt.Errorf("window does not exist")
}
// Last line in array should now be our window title, so do the compare
title := strings.Trim(outlines[len(outlines)-1], " ")
if title != tc.LastTitle {
tc.LastTitle = title
return true, nil
}
return false, nil
}