make sikuli run in background and repeat a script

Asked by rooki

i need to make a script that runs in backgrounds and checks my browser for a picture once a minute and ring a bell or start a winamp-like program with some kind of sound to alert me. the downside is that i suck at programing so i'll need a good tutorial. thks allot. i'm running win7 and using Chrome:)

Question information

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

the basic approach here:

https://answers.launchpad.net/sikuli/+question/99492

With Win7 you are on the sunny side of the town: PowerShell
http://de.wikipedia.org/wiki/Windows_PowerShell

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

Sorry, its available in english too ;-)
http://en.wikipedia.org/wiki/Windows_PowerShell

Revision history for this message
rooki (t-mann-ro) said :
#3

i found the answer to the loope last night before i posted my question but as i said early i suck at programming
i tried last night by myself but i couldn't find the commands for windows
i need something like this:
appstart("chrome.exe");
click("Image")
wait 5 secs
if ( ' image ' ) then play a song or sound
else wait 60 secs and try again

Revision history for this message
Sergey Darovskih (darovskih) said :
#4

openApp("chrome")
click("Image")
sleep(5)
while not find(' image '):
   sleep(60)
openApp("winamp")

somethinfg like that?

Revision history for this message
rooki (t-mann-ro) said :
#5

it's something like this:
openApp (chrome)
sleep(2)
click (bookmark Image )
sleep(5)
if (image) openApp (winamp)
else sleep(60)
 and loop the whole thing
-> i tried openApp("chrome") but is doesn't load i'm stuck here, does the aplication needs to be on the desktop? ore have .exe at the end ?
-> and i don't know how to loop i saw the tread that RaiMan posted early but i didin't understand it :(

Revision history for this message
rooki (t-mann-ro) said :
#6

i tried this
openApp("C:\Google\Chrome\Application\chrome.exe");
sleep(2);
click (image);
sleep(5);
while find (image):
sleep(50);
popup("working");

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

ok, try this (mind indentation, nothing to end a statement (no ;), # starts comment - its python ;-)

openApp seems to have problems. try switchApp instead (opens app if not open otherwise brings to foreground)

switchApp("chrome") # start this easy and check properties of app for correct name
sleep(2)
click (image)
sleep(5)
while find (image):
    sleep(50) # you have to indent these lines, to have them inside while
    popup("working")
# here goes what happens, if image disappears

hope it helps

Revision history for this message
rooki (t-mann-ro) said :
#8

tried with switchApp and got this error
[sikuli] [Error] Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('C:\\Users\\AppData\\Local\\Temp\\sikuli-tmp4749769730422692158.py', 5, 13, 'while find "1265238963278.png":'))

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

paste your Sikuli pls.

Revision history for this message
rooki (t-mann-ro) said :
#10

openApp("Chrome");
sleep(2)
click (picture)
sleep(5)
while find : picture
sleep(50)
popup("work!")

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

Please mind indentation, no semikolon at end of line and write it down as shown here:

switchApp("Chrome")
sleep(2)
click (picture)
sleep(5)
while find(picture):
     sleep(50)
     popup("work!")

this should work.

Revision history for this message
rooki (t-mann-ro) said :
#12

switchApp("Google Chrome")
sleep (3)
click ( )
while find( ):
sleep (50)
else popup("work!")
and it returns
[sikuli] [Error] Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', , 4, 1, 'sleep (50)'))
like it doesn't know sleep function :(

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

Sorry, you have to write your programs with python syntax!

and again mind indentation (the blanks in the beginning of some lines!!

switchApp("Google Chrome")
sleep (3)
click ( )
while find( ):
    sleep (50)
    popup("work!")

This does the following:

starts Chrome - sleeps 3 seconds - clicks on something that you know - *** then it looks for a second image - if its there, it sleeps 50 seconds - and then says by popup "work!" - and then it goes back to *** (the while) and tries to find(again).
When the image is no longer on the screen the Sikuli stops.

Revision history for this message
rooki (t-mann-ro) said :
#14

hmm i downloaded sikuli for windows keep in mind the first post i don't know verry well what i'm dooing
what i need is a script that loops
first to start chrome
switchApp("Google Chrome")
next wait 2-3 secs for chrome to load
sleep (3)
next click on a button(bookmark) from google chrome to load a page
next wait 2-3 secs for the page to load
sleep (3)
next find a specific image
find (image)
if found image wait 60 secs and then search for the image again
while find (image):
sleep(60)
if the image is missing pop an message like work
else popup("work")
this is it basicly, i'll try to find this python and bring new data if ai find anny:) thks allot for the headache you got by helping me:)

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

to get some basics about python look at http://docs.python.org/

you want to wait until the image vanishes?
Then it should look like this:

switchApp("Google Chrome")
sleep (3)
click ( )
sleep(???) # may be you need to wait for the page to be there
while find( ):
    sleep (50)
popup("work!")

so only sleep(50) is within the loop. when the loop ends (image away), the popup comes.

no headaches ;-)

Revision history for this message
rooki (t-mann-ro) said :
#16

no i want the script to keep checking the image every minute and if it disapears to show popup
and i still have a problem. the first step opening chrome doesn't work. i tried step by step and it stil doesn't load this is my first problem and i'm stuck on this step

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

no i want the script to keep checking the image every minute and if it disapears to show popup
>>> thats exactly what the script would do: sleeps along, as long as the image can be found and then wakes up (loop ends) and pops up.

the first step opening chrome doesn't work
>>> this is the case, because "Google Chrome" may not be correct. I will check it later on my Win 7. This is a chance to get Chrome on my system ;-)

you see: no headaches - only opportunities :-))

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

ok here we go (tested on windows 7, but should work with vista and xp)

chrome is not installed in the standard program path, but in the user directories. therefor Sikuli cannot find Chrome by switchApp("chrome") if Chrome is not running. (this works for all programs in the program path e.g. switchApp("explorer") switches to the next open WindowsExplorer window or opens the app).

If Chrome is already running, switchApp("chrome") brings up the Chrome window into foreground.

If Chrome is not running you have to say switchApp("... the path to chrome.exe ..."). This path you can get, if you rightclick on the "Google Chrome" - link on the desktop and select properties (last menu item (german: Eigenschaften)). In the dialog that opens, the path should already be selected (normally blue). so you can just click Strg-C to copy and then go back to Sikuli and paste (Strg-V) it into your switchApp("... paste it here ..."). This works if Chrome is not running. It works not, when Chrome is running.

weird situation.

The following works with the means of Sikuli:

switchApp("chrome") # if chrome is running it comes to foreground
if not find(... google chrome image ...): # check wether its really there
    switchApp(... the path to chrome.exe ...) # chrome not running - is started
sleep(2) # wait a little
click(... bookmark image ...)
... and so on

find(... google chrome image ...) --- this is the way, to decide, wether chrome is there. I selected the upper left corner of the chrome window about width 200 and height 100. There you have enough pixelinformation, to be shure to identify a chrome window and nothing is captured that could be different in other situations.

Revision history for this message
rooki (t-mann-ro) said :
#19

switchApp("chrome")
if not find( ):
   switchApp("C:\Users\teo\AppData\Local\Google\Chrome\Application\chrome.exe")
sleep (3)
click ( )
while find( ):
sleep (50)
else popup("work!")
and i stil get this error
SyntaxError: ('invalid syntax', ('C:\\Users\\teo\\AppData\\Local\\Temp\\sikuli-tmp4812188744024637707.py', 7, 1, 'sleep (50)'))
doest this mean sleep doesn't work with while find?

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

Once again:
Syntax error means: it is not written down according to the rules how something should be written in the programming language Python (look at some comments earlier and look at the examples at the Sikuli page). Sorry, thats how it is and its a bit tricky in Python even for people who know how to program but are new to python.

while find(): is a loop construction, that loops as long as find() is true (something found). to tell the loop, what to do (in this case sleep(50)) you have to indent it either by using the tab-key or inserting blanks before the statements inside a loop (one calls it nesting something inside).

so pls. write down exactly as shown here and it will run:

while find( ):
    sleep (50) # insert some blanks at the beginning of the line (to signal nesting!)
popup("work!") # no else! since its not nested, it is run after the loop finishes

everything after a # is taken as a comment by Python and as such ignored

One tip: if you want to have some fun with Sikuli, you should have some basic knowledge about programming especially regarding Python.

Revision history for this message
rooki (t-mann-ro) said :
#21

it's working thks allot :)
but not quite as i intended. it's working except the else part i mean it searches for the image, finds it and goes to sleep for 50 secs then shows the popup i need to show the popup if the image isin't there and loop until it finds the image
like this
1 find the image
2 image fouund wait 1 min
3 go to step 1
4 if 1 is true then popup
something like this
it does something like 1 then 2 then popup

Revision history for this message
rooki (t-mann-ro) said :
#22

sould i try with
if find image
    sleep 60
    else popup?

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

ok, one more try:

x = ... captured image ... # this is what you are looking for saved in variable x, so we can use it more than once

if find(x): # (1)
    popup("congratulations: I'm here right from the start!") # (2)
else: #(3)
    while not find(x): # (4)
        sleep(50) # (5)
    popup("Sorry for being late, but I had to sleep first!") # (6)
popup("ok, lets do our work") # (7)

since Python itself doesn't know numbering statements, I put them behind the #

How should it work:

when you start your script, it looks for the image (1).
if its already there, it executes (2) and then (7)
if its not there, it goes to (3) and looks again (4)
    loop: if found now, it pops up (6) and then (7)
    if still not found it sleeps (5) and tries again (4) (repeat from line before loop:)

again: to group actions into blocks that depend on decisions (if ... else, while, while not, ...) they have to be indented.

maybe it becomes even clearer when yo look at the screenshot: http://files.me.com/rhocke/4wri5l

Revision history for this message
rooki (t-mann-ro) said :
#24

ok i changed it a little
http://img99.imageshack.us/img99/5980/77321397.png
but it works only once doesn't go in a loop i figure because i change the page on the browser
is ther anny way for it to work in the background without bottering me?

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

ok, after a long time I think I understand your intention now:
you look at something on a webpage (bookmarked as Rating), that tells you, that you have to do something. You want Sikuli to look every minute and show up with a message, if it does not say "No rating tasks". If you have to do something, you do it and want Sikuli to go on watching.

To work, Sikuli needs the whole screen, because the actions depend on what you can see on the screen. This means, you always have to stay back, when Sikuli is doing something. Otherwise you may corrupt the situation and Sikuli wont work correctly.
This means, Sikuli will always bother you. It cannot run in the background per definition. The only possibility would be, if you can reserve some screenarea or maybe a second monitor, where Sikuli can work without being disturbed and does not disturb you.

I don't think, that Sikuli is the right solution for your needs. You need one of that tools, that run in the background and analyse a webpage about changes and report when a special situation is met. There are plenty of them available, even Firefox has a plugin for that if I remember well.

As a last idea, you can try the following:

t = 8
while t > 0:
    switchApp("chrome")
    if not find(...image to know its chrome...):
       switchApp("C:\Users\teo\AppData\Local\Google\Chrome\Application\chrome.exe")
    sleep (3)
    type("n", KEY_CTRL) # opens a new browser window
    click (...Rating-bookmark...)
    if find(...No Rating Tasks...):
       # nothing to do
       type(key.F4, KEY_CTRL) # closes the browser window again
       t = t - 1
    else:
       # something to do
       t = input("Work! Click ok or give me a 0 to stop)
       if not t: # check if only click ok
          t = t - 1
       else:
          t = int(t)

Since it opens a new browser window every hour, it gets a clean environment to do the test. But you have to interrupt your work for a short time. If nothing to do, the window vanishes again. If you have something to do, you can decide by entering 0 at the question that Sikuli should stop executing at all. When you click ok, the window stays open and you can do with it what you want. After the number of hours as given by t = at the top (here 8) the script stops on its own. If you input another number than 0 at the prompt, the script will run these numbers of hours from now on.

If you are away in the moment, the message pops up, the script waits until you come back. After the number of hours (plus may be time waiting for your ok-click) as given by t = at the top (here 8) the script stops on its own.

If you are satisfied with your script, you should save it as an executable, so you don't have the IDE visually running. The executable can be started by double clicking on it.

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

Sorry, I have to correct the last few lines:

       # something to do
       t1 = input("Work! Click ok or give me a 0 to stop)
       if not t1: # check if only click ok
          t = t - 1
       else:
          t = int(t1)

Revision history for this message
rooki (t-mann-ro) said :
#27

thks allot mate i think your right about a program that runs in the background and analyse a webpage about changes and report when a special situation is met. i found 2 of them but the page is http secure and they kinda get stuck. if you know anny good one please let me know.
thks allot

Revision history for this message
rooki (t-mann-ro) said :
#28

guess not, annyway thks allot again

Revision history for this message
Launchpad Janitor (janitor) said :
#29

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
droid8622 (droid8622) said :
#30

I have made a recursion - the skl file just launch itself at last operation)))

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

@ droid8622

how are you doing that? do you use run("...... .skl")?

If yes, every run creates a new instance (needing about 100MB), because the running new .skl is not detached (the calling .skl instance waits for completion).

you could have accomplished the same thing by just putting your workflow into a def() and using
while True:
   myWorkFlow()

even using the execfile/import options to load your specific def() at runtime.

So you don't have the startup time for each run (about 3-5 seconds) and no extra memory consumption.

So if you are doing it this way, it is not a good advice.

Revision history for this message
droid8622 (droid8622) said :
#32

I was made a doubleclick on .skl script:) Yes,I seem this was incorrect, but it was worked for me, but now, I am on linux and I am using better way - .sh script , in line 3 - "145" is amount of cycles

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {1..145}
  do
     echo "Welcome $i times"
/home/al/path/to/Sikuli-IDE/sikuli-script.jar /home/al/path/to/script.sikuli
 done