RaiMan / SikuliX1

SikuliX version 2.0.0+ (2019+)
https://sikulix.github.io
MIT License
2.72k stars 349 forks source link

Application not Activating on MacOS #386

Closed scottschreckengaust closed 3 years ago

scottschreckengaust commented 3 years ago

The application does not activate although it is running and has a window. The generated osascript does not adhere to the requested application (see the debug output below).

NOTE: This particular application is an application generally in the background and does not show on the taskbar unless opened.

FYI: An AppleScript with tell application "OpenVPN Connect" to activate works IF it is open

import sys
import os
from java.lang import System

Debug.on(3)
setShowActions(True)
Debug.user("OS: " + str(Env.getOS()))
Debug.user("Version: " + Env.getOSVersion())
Debug.user("Sikuli: " + Env.getSikuliVersion())
Debug.user("Python: " + sys.version.replace('\n', '') + ' on ' + sys.platform)
Debug.user("Java: " + System.getProperty('java.version'))
for screen_index in range(getNumberScreens()):
        Debug.user("Screen " + str(screen_index) + ": " + str(Screen(screen_index).getBounds()))

name_of_app = r'"OpenVPN Connect"'
my_app = App(name_of_app)

if not my_app.window():
    Debug.user("no window, so opening...and setting...")
    my_app = App.open(name_of_app)
else:
    Debug.user("attempting to focus..." + name_of_app)
    if switchApp(name_of_app):
        Debug.user("switched using 'switchApp'")

Debug.user(my_app.getName())
if my_app.isRunning(0):
    Debug.user("isRunning")
    if my_app.isValid():
        Debug.user("isValid") 
        Debug.user(str(my_app.getPID()))

if my_app.hasWindow():
    Debug.user("hasWindow")
    Debug.user(my_app.getTitle())
    my_window = my_app.window()

    Debug.user(str(my_window.getScreen().getBounds()))

    Debug.user(str(my_window.getX()))
    Debug.user(str(my_window.getY()))
    Debug.user(str(my_window.getW()))
    Debug.user(str(my_window.getH()))

Output:

[debug] IDE: MenuAction.runSelection
[debug] Jython: use as default region: R[0,0 1440x900]@S(0)
[debug] Image: BufferedImage: (1440, 900)
[user (10/28/20, 12:29:48 PM)] OS: MAC
[user (10/28/20, 12:29:48 PM)] Version: 10.15.7
[user (10/28/20, 12:29:48 PM)] Sikuli: 2.0.4
[user (10/28/20, 12:29:48 PM)] Python: 2.7.2 (v2.7.2:925a3cc3b49d, Mar 21 2020, 10:03:58)[OpenJDK 64-Bit Server VM (Amazon.com Inc.)] on java11.0.8
[user (10/28/20, 12:29:48 PM)] Java: 11.0.8
[user (10/28/20, 12:29:48 PM)] Screen 0: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
[user (10/28/20, 12:29:48 PM)] Screen 1: java.awt.Rectangle[x=1440,y=-514,width=1920,height=1080]
[user (10/28/20, 12:29:48 PM)] attempting to focus..."OpenVPN Connect"
[debug] AppleScriptRunner: AppleScript:
#!/usr/bin/osascript
tell application "OpenVPNConnect" to activate
returned:
*****error*****
/var/folders/rn/lxr8csm50jjcxwz2y5pg4h880000gn/T/Sikulix_946564600/sikulitemp-16229151876281500024.script:37:45: execution error: Can’t get application "OpenVPNConnect". (-1728)

[user (10/28/20, 12:29:50 PM)] switched using 'switchApp'
[user (10/28/20, 12:29:50 PM)] OpenVPN Connect
[user (10/28/20, 12:29:57 PM)] isRunning
[user (10/28/20, 12:29:57 PM)] isValid
[user (10/28/20, 12:29:57 PM)] 60268
[user (10/28/20, 12:29:57 PM)] hasWindow
[user (10/28/20, 12:29:57 PM)] OpenVPN Connect
[user (10/28/20, 12:29:57 PM)] java.awt.Rectangle[x=0,y=0,width=1440,height=900]
[user (10/28/20, 12:29:57 PM)] 605
[user (10/28/20, 12:29:57 PM)] 60
[user (10/28/20, 12:29:57 PM)] 400
[user (10/28/20, 12:29:57 PM)] 685

Somehow the AppleScriptRunner retrieves a name different from the app.getName() as output shows above during the switchApp?

https://github.com/RaiMan/SikuliX1/blob/108f9acc739b0ae97c7131b98d7ec9c6b4f728d3/API/src/main/java/org/sikuli/natives/MacUtil.java#L149

RaiMan commented 3 years ago

Impressing doc ;-) Will have a look. Thanks.

scottschreckengaust commented 3 years ago

Here is the download page for "OpenVPN Connect" https://openvpn.net/download-open-vpn/

scottschreckengaust commented 3 years ago

...and the version installed: https://swupdate.openvpn.net/downloads/connect/openvpn-connect-3.2.1.1484_signed.dmg

RaiMan commented 3 years ago

I do not have any problems.

app = App("OpenVPN Connect")
if app.isRunning():
    # both variants work
    #app.focus()
    switchApp(app.getName())
else:
    print "not running"
    app.open() # works
scottschreckengaust commented 3 years ago

Your example script runs, but adding the Debug.on(3) statement still shows that the AppleScript errors each time and subsequent runs when the window is not visible fail.

More importantly, after the first run (which opens the window to the foreground), if the window is either sent behind another window or is closed (not exited) as the application stays running in the background, a second Sikuli run of the same above script does not have the window appear. Therefore, a find or click will then not work as expected (because it is not visible).

Debug.on(3)
app = App("OpenVPN Connect")
if app.isRunning():
    # both variants work
    #app.focus()
    switchApp(app.getName())
else:
    print "not running"
    app.open() # works
[debug] AppleScriptRunner: AppleScript:
#!/usr/bin/osascript
tell application "OpenVPNConnect" to activate
returned:
*****error*****
/var/folders/rn/lxr8csm50jjcxwz2y5pg4h880000gn/T/Sikulix_475142172/sikulitemp-13007145423498924402.script:37:45: execution error: Can’t get application "OpenVPNConnect". (-1728)

Could it be that the generated AppleScript error is missing the space between "OpenVPN" and "Connect"?

RaiMan commented 3 years ago

Thanks for feedback.

I guess, I have to go a bit deeper ;-)

RaiMan commented 3 years ago

Again: I do not have any problems using the above script:

I did not find any place, where the space character might be lost on the way down from executing switchApp().