Skip to content

Commit

Permalink
bring to front
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Dec 21, 2023
1 parent 252f3f5 commit 76561bc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/watcher/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -13,6 +14,7 @@ import (
"log/slog"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -140,6 +142,10 @@ func TestDaemon(t *testing.T) {
if err != nil {
t.Errorf("failed to start terminal %d", i)
}
err = bringWindowToFront("tester", fmt.Sprintf("tester:%d", i))
if err != nil {
t.Logf("bring: %v", err)
}
t.Cleanup(func() {
cmd.Process.Kill()
})
Expand Down Expand Up @@ -214,3 +220,25 @@ func TestDaemon(t *testing.T) {
func ptr[T any](v T) *T {
return &v
}

func bringWindowToFront(application, title string) error {
if runtime.GOOS != "darwin" {
return nil
}
cmd := execabs.Command("osascript", "-e", fmt.Sprintf(`
tell application "%s"
set index of window whose name is "%s" to 1
end tell
tell application "System Events" to tell process "%[1]s"
perform action "AXRaise" of window 1
end tell
`, application, title))
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("%w: %s", err, &stderr)
}
return nil
}

0 comments on commit 76561bc

Please sign in to comment.