[request] Windows: want better support with App.focus() and switchApp()?

Bug #1008113 reported by ABET

This bug report was converted into a question: question #199255: bug on focus() or switchApp?.

24
This bug affects 4 people
Affects Status Importance Assigned to Milestone
SikuliX
In Progress
Medium
RaiMan

Bug Description

Is there some bug with focus() or switchApp() handled on windows? I noticed that if I use the same method on mac it works find and puts the application in the front, but when using it on windows it does not.

what I did was put the application (ff) behind a different application (ie) and want to get a focus on it so it becomes the application in front.
code:
switchApp('firefox')

or

ff = App('firefox')
ff.focus()

- another q, is that if the application is minimized (win or mac), would focus() also work and open the application and put it in focus()?

Revision history for this message
RaiMan (raimund-hocke) wrote :

This is not a bug, but behavior based on the selected solutions in the different systems, which are totally different on Mac and Windows.

--- on Mac
you really focus an application (which might not even run), but you have no chance to tell a special window to come to front.
The frontmost window depends on the "history" of the application's windows.
So in the case of all windows of the application being hidden, the last one, that was hidden, will come up with focus.
If at least one window is visible, focus will not change (and cannot do it with Sikuli features) anything on the windows's z-order.

--- on Windows
Handling of running applications is done based on identifying part of the window title. This is special with FF in some versions, since FF might not have any window titles (see faq 1667, faq 1730 might be of interest too).
Hidden (minimized) windows are not recognized by Sikuli currently.

--- Conclusion:
If you make scripts, that should run on Windows and Mac, you have to script different actions for each system (see docs additionally).

Revision history for this message
RaiMan (raimund-hocke) wrote :

not a bug

Changed in sikuli:
status: New → Invalid
Revision history for this message
ABET (abetv) wrote :

Hi thanks. I did put a condition to handle windows machines since that is what I've read from the FAQs, but instead of seeing there the apps switching to the front it is not doing so unlike the mac.

try this:
Manager is the task manager. I launched both task manager and firefox, where firefox may put the task manager behind it bec of the focus.
a = App('Manager')
b = app('firefox')

b.focus()
wait(2)
a.focus()
wait(2)
b.focus()
wait(2)
a.focus()
wait(2)

You will see that either task manager or firefox will not be put in front and will always be behind the other app

Revision history for this message
ABET (abetv) wrote :

oops typo. that should be b = App('firefox')

Note if I do the same on the mac, I would see the app switching. Unlike windows.
mac:
a = App('chess')
b = App('firefox')

Revision history for this message
RaiMan (raimund-hocke) wrote :

I cannot reproduce your problem on my Win7 installation.

It works fine for me.

Revision history for this message
ABET (abetv) wrote :

Interesting. Wonder if this issue is only for XP. Since I also noticed that sometimes the focus would stop working and I would have to reboot the machine for it to work again.

Revision history for this message
RaiMan (raimund-hocke) wrote :

Might be that this is special for WinXP.

summary: - bug on focus() or switchApp?
+ Windows XP: bug on focus() or switchApp?
Revision history for this message
Benny Phanichkul (benny-w) wrote : Re: Windows XP: bug on focus() or switchApp?

I see the same behavior with Manager and Firefox. I'm on Win7SP1 32bit. Used the same sample code in comment #3 and focus does not switch back and forth between the applications. However, if i switch between Notepad and VLC Media Player, it works fine.

Revision history for this message
RaiMan (raimund-hocke) wrote :

Ok, I think this has something to do with the bunch of invisible windows, that FF has open.

If you try this with FF, you can see this:

ff = App("firefox")
for i in range(100):
    w = ff.window(i)
    if not w: break
    print w

So before running the snippet, manually make a "real" FF window the frontmost, and I think it should work.
I have always tested this way and it worked for me, even if the FF window was somewhere behind other windows.

When I am right, then there is currently no solution for that, because it is not possible, to get a special window to the front by saying:
ff.window(4).focus()

@Benny
please paste here, how you did your "switch between Notepad and VLC Media Player"

Revision history for this message
Benny Phanichkul (benny-w) wrote :

I just took the snippet and replaced Manager and Firefox with Notepad and VLC. The following code works for me without failure on 32bit win7

a = App('Notepad')
b = App('VLC')

b.focus()
wait(2)
a.focus()
wait(2)
b.focus()
wait(2)
a.focus()
wait(2)

@Raiman, I tried manually opening a Firefox window (no other tabs open) and attempting to switch back and forth with VLC and it fails when switching from VLC to Firefox the 2nd time (ie the second a.focus() fails). Here's the snippet I used and also the log.

Snippet
-------------------
a = App('Firefox')
b = App('VLC')

b.focus()
wait(2)
a.focus()
wait(2)
b.focus()
wait(2)
a.focus()
wait(2)

Log
------------------
[log] App.focus VLC(0) #0

[log] App.focus Firefox(0) #0

[log] App.focus VLC(7332) #0

[log] App.focus Firefox(6624) #0

Revision history for this message
RaiMan (raimund-hocke) wrote :

Thanks for test and feedback, I will have a deeper look at it, but no idea at the moment.

FF version?

Revision history for this message
Benny Phanichkul (benny-w) wrote :

i'm using FF13

Revision history for this message
UNCCheezy (shawny2005) wrote :

what a pain windows. I'm having similar issues with chrome. Photoshop works fine, chrome not so much.

Revision history for this message
RaiMan (raimund-hocke) wrote :

@ all who might be interested:
I will try to ease the pain a bit during the next months.

Please post failing situations with detailed information.

Changed in sikuli:
status: Invalid → In Progress
importance: Undecided → Medium
assignee: nobody → RaiMan (raimund-hocke)
milestone: none → 1.2.0
summary: - Windows XP: bug on focus() or switchApp?
+ [request] Windows: want better support with App.focus() and
+ switchApp()?
Revision history for this message
Assaf Mendelson (a-mendelson) wrote :

I have been trying to use this same basic schema:
a = App(r"C:\Program Files (x86)\eMule\emule.exe")
b = App(r"C:\Program Files (x86)\Steame\steam.exe")
wait(5)
a.focus()
wait(2)
b.focus()
wait(2)
a.focus()
wait(2)
b.focus()
wait(2)

When both steam and emule are turned off it creates both, brings steam to focus but does not bring emule to focus again. The log says:
[log] App.focus C:\Program Files (x86)\Steam\steam.exe(0) #0

[log] App.focus c:\Program Files (x86)\eMule\emule.exe(0) #0

[log] App.focus C:\Program Files (x86)\Steam\steam.exe(5420) #0

[log] App.focus c:\Program Files (x86)\eMule\emule.exe(3660) #0

When both are already working it does not create a new versions of then and does not change the focus. The log says:
[log] App.focus C:\Program Files (x86)\Steam\steam.exe(0) #0

[log] App.focus c:\Program Files (x86)\eMule\emule.exe(0) #0

[log] App.focus C:\Program Files (x86)\Steam\steam.exe(4804) #0
[error] App.focus failed: C:\Program Files (x86)\Steam\steam.exe(4804) not found

[log] App.focus c:\Program Files (x86)\eMule\emule.exe(4192) #0
[error] App.focus failed: c:\Program Files (x86)\eMule\emule.exe(4192) not found

Revision history for this message
Assaf Mendelson (a-mendelson) wrote :

BTW I also tried:
a = App(r"C:\Program Files (x86)\eMule\emule.exe")
b = App(r"C:\Program Files (x86)\Steame\steam.exe")
a.open()
b.open()
wait(5)
c = b.window()
if not b:
    popup("not found")
    exit(1)
popup("found")
When the applications are not running i get a "found" (i.e. it finds the window and object) but when the applications are already running i get a "not found"

RaiMan (raimund-hocke)
Changed in sikuli:
milestone: 2.0.0 → 2.1.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

Bug watches keep track of this bug in other bug trackers.