zenity: [macOS] Dialogs don't get focus automatically

Test code:

package main

import (
	"fmt"
	"github.com/ncruces/zenity"
)

func main() {
	file, err := zenity.SelectFile(zenity.Title("Test focus on MacOS"))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(file)
}

Run in iTerm2, the SelectFile dialog does not get focus unless click the title bar. Similar to https://discussions.apple.com/thread/2548172

Run in native Terminal, the SelectFile dialog get focus with one click. Is it possible to get focus automatically?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 32 (15 by maintainers)

Commits related to this issue

Most upvoted comments

zenity.WindowIcon(icon) is in master solving all focus issues.

I’ll have a go at zenity.Attach(id) where id can be:

  • a PID (int) or app name (string) on macOS
  • a HWND (uintptr) on Windows
  • a window id (uint32) on Unix

It’ll be the app’s responsibility to figure out what to attach to (the zenity command might try to attach to a parent terminal).

How about this? It works on my macOS.

import "golang.org/x/sys/unix"

func GetParentWindowId() int {
	pid := os.Getppid()
	for {
		kinfo, err := unix.SysctlKinfoProc("kern.proc.pid", pid)
		if err != nil {
			return 0
		}
		switch kinfo.Eproc.Ppid {
		case 0:
			return 0
		case 1:
			return pid
		default:
			pid = int(kinfo.Eproc.Ppid)
		}
	}
}