switchApp()

Asked by Eric Jordan on 2019-05-10

VS 1.1.3
Win10 64bit
I can use the software as normal with what seems like all the methods, but as soon as I use switchApp() it fails to run.

I have tried this tutorial: https://www.youtube.com/watch?v=xTTIikzxFfc I have the exact same code as him and mine will not work at all.

I have tried a few google chrome screens

A couple other games.

This will not work at all.

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2019-05-10
Last reply:
2019-05-17

This question was originally filed as bug #1828501.

RaiMan (raimund-hocke) said : #1

such things should always start as a question.

RaiMan (raimund-hocke) said : #2

just tried with Win10-64 and Java 8 and SikuliX 1.1.3
having the Click-Heroes game open in GoogleChrome
switchApp("Click Heroes")
does what it should: brings the Chrome-Window to front.
with a slow machine just put a wait(5) after the switchApp

RaiMan (raimund-hocke) said : #3

For clicker heroes I am not using full screen, it launches in a little 600x600 screen and I am just keeping it like that. I do use multi monitor setup.

What snippet do you need from me?

RaiMan (raimund-hocke) said : #4

please follow this thread.

I want to see the code you are using to prepare and use switchApp().

What title do you see on the window showing the game?

Eric Jordan (forensicsman) said : #5

This is the same exact code as the link I showed you in the first post:

the title on the window shows Clicker Heroes.

Settings.MoveMouseDelay = 0.0
running = True
shouldClickFor = 50
shouldUpgrade = False
timeToDead = 0
boss = False

def runHotkey(event):
    global running
running = False

Env.addHotkey(Key.F1, KeyModifier.CTRL, runHotkey)
ch = switchApp("Clicker Heroes")
wait(5)
while(ch.hasWindow() and running):
    if shouldClickFor > 0:
        hover(Pattern("1557442376919.png").similar(0.89).targetOffset(-209,-223))
while shouldClickFor > 0 and running:
    mouseDown(Button.LEFT)
    mouseUp(Button.LEFT)
    shouldClickFor -= 1
    if timeToDead >= 2 and not exists(Pattern("1557442586882.png").similar(0.90)):
        click(Pattern("1557444795030.png").similar(0.89).targetOffset(-143,0))
        timeToDead = 0
        if exists(Pattern("1557442917111.png").similar(0.89)):
            shouldUpgrade = True
        click(Pattern("1557442917111.png").similar(0.89).targetOffset(51,0))
        timeToDead += 1
    else:
        if not shouldUpgrade:
            timeToDead = 0
    shouldUpgrade = False
    shouldClickFor = 50
    if exists(Pattern("1557444278907.png").similar(0.90)) and not boss:
        shouldClickFor = 150
        boss = True
    elif exists(Pattern("1557444278907.png").similar(0.90)):
         click(Pattern("1557442759904.png").similar(0.91).targetOffset(-251,-1))
boss = False

RaiMan (raimund-hocke) said : #6

I againntaested with your environment (Win10-64, Java 8, SikuliX 1.1.3):

Clicker herous window is visible in a Chrome tab, which has in title "Clickker heroes"

This is my test:
app = switchApp("Clicker Heroes")

if not app.isRunning():
    exit(1)
if app.hasWindow():
    print "window:", app.window()
game = App.focusedWindow()
use(game)
highlight(2)
imgClickPoint = Pattern("imgClickPoint.png").targetOffset(-138,-129)
clickPoint = find(imgClickPoint)
hover(clickPoint)
for i in range(100):
    mouseDown(Button.LEFT)
    mouseUp()

wait(3)
print "************** The End"

BTW: I recommend to try with 1.1.4

Eric Jordan (forensicsman) said : #7

Hello,

I got home today and DL 1.1.4 and ran it and used your code and it worked!

So I tried another game and I used your code and just tweaked to do another check and I received this error:

[error] RobotDesktop: checkMousePosition: should be L[940,1216]@S(0) but after move is L[2886,122]@S(0) Possible cause in case you did not touch the mouse while script was running: Mouse actions are blocked generally or by the frontmost application. You might try to run the SikuliX stuff as admin.

I tried to right click the jar file but it does not allow me to run the file as admin and I tried a google search and was not able to find an exact fix.

there was an old forum post on here I found, but I had no idea what his fix was.

RaiMan (raimund-hocke) said : #8

--- [error] RobotDesktop: checkMousePosition: ...
this typical for situations, where the mouse actions done by SikuliX/Java-Robot are blocked by the System/Application.

If directly running the sikulix.jar as admin does not work, open an administrator-commandline-window and use
java -jar <path-to>/sikulix.jar

RaiMan (raimund-hocke) said : #9

if this does not work, then there is no solution for the mouse-blocking-problem with SikuliX.

Eric Jordan (forensicsman) said : #10

Hello,

your suggestion worked!

so, I was practicing again trying to learn to send keyboard commands and now I got this error: I tried to google it and there was millions of fixes but I dont understand what it meant to fix it.

[error] SyntaxError ( "mismatched input '' expecting DEDENT", )

here is my code:

Settings.MoveMouseDelay = 0.0
def runHotkey(event):
    global running
    running = False
running = True
Env.addHotkey(Key.F1, KeyModifier.CTRL, runHotkey)

BR = switchApp("Blood Rites")

if not BR.isRunning():
    exit(1)
if BR.hasWindow():
    print "window:", BR.window()
game = App.focusedWindow()
use(game)
highlight(1)

while(BR.hasWindow() and running):
   if exists(Pattern("1557877521896.png").similar(0.89)):
print "Found spot"
    keyDown("1")
    keyUp()

  # click(Pattern("1557878772334.png").similar(0.90))

  # imgClickPoint = Pattern("imgClickPoint.png").similar(0.89)
#clickPoint = find(imgClickPoint)
#hover(clickPoint)
#for i in range(1):
 # mouseDown(Button.LEFT)
  # mouseUp()

wait(2)
print "************** The End"

RaiMan (raimund-hocke) said : #11

basic python scripting knowledge:
Python uses indentation to separate blocks of code.

start here: faq 1800

Eric Jordan (forensicsman) said : #12

Hello,

I am a hobby programmer and I have never seen a language do this type of thing. I was able to get it back working. Thank you.

What is the best way with Sikuli to have it watch for a pixel to change from white to green?

RaiMan (raimund-hocke) said : #13

Please start a new question for a new topic.
New questions are seen by the community, whereas comments on questions are only seen by the creator, other subscribers and me.

pixelcolor:
if you want to work with screen colors, you need to know the structure of the RGB color scheme.

SikuliX has a function:

color = Location(x,y).getColor()

color now is a color object, that you can evaluate:
red = color.getRed()
green = color.getGreen()
blue = color.getBlue()

rgb = color.getRGB()

now you have the color components

waiting for color change:

oldColor = someLocation.getColor()
while True:
    newColor = someLocation.getColor()
    if oldCor.getRGB() == newColor.getRGB():
        wait(1) # wait 1 sec
        continue # and check again
    break # color has changed - end the loop

Eric Jordan (forensicsman) said : #14

Hello,

I know this is a bug area, where do we go to just ask questions?

I do appreciate you helping me. There are so many forums that are so toxic for beginners.

Since you answered me here I will show you this and then continue in the question area.

I used your example up top:

oldColor = Location(1901,1096).getColor()
rgb = oldColor.getRGB()
while True:
    newColor = Location(1901, 1096).getColor()
    if oldColor.getRGB() == newColor.RGB():
        popup("Color has changed")

I get this error:
[error] Error caused by: Traceback (most recent call last): File "C:\SikuliX\Sikuli 1.4\Scripts\WGT.sikuli\WGT.py", line 20, in <module> if oldColor.getRGB() == newColor.RGB(): TypeError: 'int' object is not callable

RaiMan (raimund-hocke) said : #15

This thread already is in the Question&Answer area (I switched it from the bug area earlier).

At top right click "Ask a question" and you will be able to do so ;-)

Your problem:

if oldColor.getRGB() == newColor.RGB():

... left out the "get" at ".RGB" in the right expression.

it must either be:

if oldColor.getRGB() == newColor.getRGB():

or

if oldColor.RGB == newColor.RGB:

The magic behind:

getRGB returns an integer value and is a function (callable) and hence has to be written as getRGB()

as a Jython-Java convenience you can use .RGB, since at the Java level there is a so called getter function getRGB() found by Jython.

when writing color.RGB, this already is a value (integer) and hence not callable, so you have to leave out the brackets.

Can you help with this problem?

Provide an answer of your own, or ask Eric Jordan for more information if necessary.

To post a message you must log in.