go-vgo / robotgo

RobotGo, Go Native cross-platform RPA and GUI automation @vcaesar
Apache License 2.0
9.68k stars 887 forks source link

GetBounds returns wrong values #210

Open elonzh opened 5 years ago

elonzh commented 5 years ago
$go version  
go version go1.12 darwin/amd64
$gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
package main

import (
    "fmt"
    "strings"

    "github.com/go-vgo/robotgo"
)

func main() {
    fmt.Println("Version:", robotgo.Version)
    process, _ := robotgo.Process()
    for _, p := range process {
        if strings.Contains(strings.ToLower(p.Name), "macpass") {
            mx, my := robotgo.GetMousePos()
            x, y, w, h := robotgo.GetBounds(p.Pid)
            robotgo.SetActive(robotgo.GetHandPid(p.Pid))
            fmt.Println("Bounds:", robotgo.IsValid(), x, y, w, h)
            fmt.Println("MousePos:", mx, my)
            bitmap := robotgo.CaptureScreen()
            defer robotgo.FreeBitmap(bitmap)
            robotgo.SaveBitmap(bitmap, "test.png")
        }
    }
}
$go run main.go                                                                       
Version: v0.80.0.845, Sierra Nevada!
Bounds: true 974848 512 -1 197
MousePos: 509 650

Description

test

bt commented 5 years ago

Also having the same issue, any investigation done?

amlwwalker commented 5 years ago

Has this progressed?

amlwwalker commented 5 years ago

I researched this further as was getting no response here. Turns out osx will return nil when you ask for the bounds of another application and therefore this is not possible.

caryyu commented 4 years ago

OS: Ubuntu 16.04 Description: the value of X variable is obviously greater than screen size

Code

    fpid, err := robotgo.FindIds("Game")
    if err != nil {
        log.Fatal(err)
    }
    pid := fpid[0]
    fmt.Println("pid: ", pid)
    x, y, w, h := robotgo.GetBounds(pid)
    fmt.Printf("x: %d, y: %d, w: %d, h: %d \n", x, y, w, h)
    sW, sH := robotgo.GetScreenSize()
    fmt.Printf("screen size: %d, %d", sW, sH)

Output

pid:  11806
x: 2555, y: 150, w: 1024, h: 796 
screen size: 1920, 1080
markgician commented 4 years ago

have the same problem. how to test/debug where goes wrong in win_sys.h Bounds get_bounds(uintptr pid, uintptr isHwnd){

J7mbo commented 5 months ago

Running the exact code from @caryyu on MacOS (M3) gives:

x: 3629056, y: 3627784, w: 1668311156, h: -1177118969

That seems just a little bit off to me.


As this will likely never be fixed, I cheated and called out to AppleScript to get the x, y, w, h. Using Mack:

str, err := mack.Tell("System Events", `
tell application process "<<window_name_goes_here>>"
    get { position, size } of window 1
end tell
`)
fmt.Println(str) // 0, 38, 1514, 884
zyxkad commented 2 months ago

Same problem, as well as on macos an application may have multiple window appears on multiple desktop. I think the API should also fit that.