How to use switchApp() to check whether a specified is opened or not

Asked by Parn Yin

I want to use switchApp() to check whether a specified window in my company's application is opened or not.
The goal is:
    If it's opened, script continue.
    If not, throw an exception.
I don't want to use exists() or wait() with screenshots because of some reasons in the application I am going to test with.

However, I found that script continue even the window can't be found by switchApp().
Although red line is displayed in the console:
    [error] App.focus failed: Calculator not found

setThrowException() can't work too.

So I use this logic in my script:

cat = switchApp("Calculator") # Here I use Calculator instead of my company's application
if (cat == 0):
    print "continue"
else:
    raise Exception("NO!!!")

Excuse me, is there a better way please?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

Sorry, currently no other option with Sikuli's features.

Revision history for this message
Techno.Scavenger (techno-scavenger) said :
#2

If you are on Windows, you can do something like:

import subprocess
p = subprocess.Popen('TASKLIST /FI "IMAGENAME eq notepad.exe" | findstr notepad',shell=True)
p.wait

if(p.returncode == 0):
  do something
else:
 do other stuff

Revision history for this message
Techno.Scavenger (techno-scavenger) said :
#3

Ok, looks jython behaves different with respect to pipes... you can try this.

import subprocess
px = subprocess.Popen("tasklist",stdout=subprocess.PIPE)
py = subprocess.Popen("findstr notepad",stdin=px.stdout,stdout=subprocess.PIPE)
px.stdout.close()
out = py.communicate()[0]
py.returncode
#assuming notepad is not running, this will return 1

#now open notepad
px = subprocess.Popen("tasklist",stdout=subprocess.PIPE)
py = subprocess.Popen("findstr notepad",stdin=px.stdout,stdout=subprocess.PIPE)
px.stdout.close()
out = py.communicate()[0]
py.returncode
#this returns zero.

Revision history for this message
Parn Yin (pyin) said :
#4

Thanks RaiMan, that solved my question.