[2.0.5] Java program: Unable to bring window forward on Windows 10

Asked by Jeff Harmon

***** accepted as bug: follow up on GitHub: https://github.com/RaiMan/SikuliX1/issues/464
-------------------------------------------------------------

***** workaround:
A workaround is to disable the taskbar icon flashing completely:
https://www.thewindowsclub.com/disable-flashing-taskbar-buttons-windows
Be sure to set both values to 0 (zero).
--------------------------------------------------------------

I am trying to bring an application that is already running into the foreground on Windows 10 using Java. Here is the code I am using:

App app = App.open("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
app.focus();

This causes the Notepad++ application to blink in the taskbar, but it doesn't bring it to the foreground. What am I missing?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jeff Harmon (phototacopodcast) said :
#1

BTW, the following in SikuliX IDE works just fine to bring the application to the foreground:

app = App.open("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
app.focus();

Revision history for this message
RaiMan (raimund-hocke) said :
#2

app = new App("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
app.open();

... brings notepad++ to front in all cases:
not open, open and even minimized.

Revision history for this message
Jeff Harmon (phototacopodcast) said :
#3

I am still having trouble with this. In Java this works if the application isn't open or with the first call, but it fails very regularly after that. I am also having trouble getting anything in the debug logs.

Here is the java code:

    public static void main(String[] args) {
        Debug.setLogFile("C:\\Users\\Jeff\\SikulixDebug.log");
        Debug.on(3);
        String appName = "\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"";
        App test = new App(appName);
        boolean opened = test.open();
        test.focus();
        if (!opened || !test.hasFocus()) {
            System.err.println("test: Failed to open "+appName+", opened="+opened+",hasFocus="+test.hasFocus());
        } else {
            System.out.println("test: Successfully opened " + appName);
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        test = new App(appName);
        opened = test.open();
        test.focus();
        if (!opened || !test.hasFocus()) {
            System.err.println("test: Failed to open "+appName+", opened="+opened+",hasFocus="+test.hasFocus());
        } else {
            System.out.println("test: Successfully opened " + appName);
        }

Here is the log from the above:

test: Successfully opened "C:\Program Files (x86)\Notepad++\notepad++.exe"
test: Failed to open "C:\Program Files (x86)\Notepad++\notepad++.exe", opened=true,hasFocus=false

The SikulixDebug.log file is created at C:\User\Jeff\SikulixDebug.log but is empty

The use case for me is that I need to make sure the app is in focus multiple times and it fails more than it works.

Java 11, Sikulix API 2.0.5

Revision history for this message
Jeff Harmon (phototacopodcast) said :
#4

I should have added that while the 5 second wait is taking place in the Java program, I bring another application to the foreground.

Revision history for this message
Jeff Harmon (phototacopodcast) said :
#5

Another use case to illustrate what I am seeing. Here is Java code top use App.open to bring Notepad++ to the foreground when already running (works), waits 200ms, uses App.open to bring MS Edge to the foreground when already runinng, waits 200ms, and then tries to bring Notepad++ to the foreground again.

public static void main(String[] args) throws InterruptedException {
        Debug.setDebugLevel(3);
        Settings.ActionLogs = true;
        Settings.InfoLogs = true;
        Settings.DebugLogs = true;
        Debug.setLogFile("C:\\Users\\Jeff\\SikulixDebug.log");
        Debug.on(3);
        String notepadPPName = "\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"";
        App notepadPP = new App(notepadPPName);
        boolean opened = notepadPP.open();
        notepadPP.focus();
        if (!opened || !notepadPP.hasFocus()) {
            System.err.println("test: Failed to open "+notepadPPName+", opened="+opened+",hasFocus="+notepadPP.hasFocus());
        } else {
            System.out.println("test: Successfully opened " + notepadPPName);
        }

        Thread.sleep(200);

        String edgeBrowserName = "\"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe\"";
        App edgeBrowser = new App(edgeBrowserName);
        opened = edgeBrowser.open();
        if (!opened || !edgeBrowser.hasFocus()) {
            System.err.println("test: Failed to open "+edgeBrowserName+", opened="+opened+",hasFocus="+edgeBrowser.hasFocus());
        } else {
            System.out.println("test: Successfully opened " + edgeBrowserName);
        }

        Thread.sleep(200);

        notepadPP = new App(notepadPPName);
        opened = notepadPP.open();
        notepadPP.focus();
        if (!opened || !notepadPP.hasFocus()) {
            System.err.println("test: Failed to open "+notepadPPName+", opened="+opened+",hasFocus="+notepadPP.hasFocus());
        } else {
            System.out.println("test: Successfully opened " + notepadPPName);
        }

    }

Actual result is MSEdge never comes to the foreground. Here are the outputs:

test: Successfully opened "C:\Program Files (x86)\Notepad++\notepad++.exe"
test: Failed to open "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", opened=true,hasFocus=false
test: Successfully opened "C:\Program Files (x86)\Notepad++\notepad++.exe"

And I do get a SikulixDebug.log file that has:

[debug] Debug: setLogFile: C:\Users\Jeff\SikulixDebug.log

Revision history for this message
Jeff Harmon (phototacopodcast) said :
#6

This Jython code in the SikulixIDE 2.0.4 that does the same thing as the Java code (opens Notepad++, then Edge, then Notepad++) works exactly as expected

app1 = App(r'"C:\\Program Files (x86)\\Notepad++\\notepad++.exe"')
opened = app1.open()
sleep(2)
app2 = App(r'"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"')
app2.open()
sleep(2)
app1.open()
sleep(2)

What am I missing why the Java code using skikulixapi 2.0.5 is not working like the jython code in SikuliXIDE?

Revision history for this message
RaiMan (raimund-hocke) said :
#7

Ok, confirmed.

In the IDE (tested with 2.0.5) everything works as expected, even when the script is run from commandline.

Using the same workflow in a Java program, only the first focus works.
All following only make the icon in the taskbar blinking.

I currently have no idea, what causes the different behavior, but will try to find out.

2.0.6 will have a different implementation of the App class. I will check its behavior.

for now I accept it as a bug (follow up on GitHub).

Can you help with this problem?

Provide an answer of your own, or ask Jeff Harmon for more information if necessary.

To post a message you must log in.