From 76561bcce6d1b3c5440fd3873ba4634f72b3a7b1 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 22 Dec 2023 09:47:16 +1030 Subject: [PATCH] bring to front --- cmd/watcher/main_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/watcher/main_test.go b/cmd/watcher/main_test.go index fe0a174..feea191 100644 --- a/cmd/watcher/main_test.go +++ b/cmd/watcher/main_test.go @@ -5,6 +5,7 @@ package main import ( + "bytes" "context" "encoding/json" "errors" @@ -13,6 +14,7 @@ import ( "log/slog" "os" "path/filepath" + "runtime" "sync" "testing" "time" @@ -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() }) @@ -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 +}