← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #156488]: Sleep function for certain time of day

 

Question #156488 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/156488

    Status: Open => Answered

RaiMan proposed the following answer:
Uuups, that was too fast4you ;-)

But do not mind, being a newbie: if you stay on the road, we will get it
together :-)

--1. The syntax of the language used is Python. So if you want to do
yourself some good support, have a look at the first part of the e-book
at: http://jythonpodcast.hostjava.net/jythonbook/en/1.0/ and at the
Sikuli docs at: http://sikuli.org/docx/

--2. In such a scripting language some syntax is defined, that tells the
Interpreter (the thing, that is running your script), which statements
belong together, so that they form some block, that is either processed
together or not at all.

the statements beginning with def sleepUntil(h, m, s=0): to wait(diff)
form such a block. It is a function definition, that can be called using
e.g. sleepUntil(14, 30).

the def statement starts the block and it is left justified. All the
following statements including wait have to be indented one level: in
front of the first character there has to be one tab (never use blanks
for that) this is easily accomplished by selecting all lines and
pressing tab one time.

--3. now we want to use the function (this is what I meant with usage above):
back to left justified (no indent):
sleepUntil(14, 30) # this calls the function and makes the script wait until 14:30
click("some-image.png")

--conclusion
your script should look like this:

# start of copy/paste block
def sleepUntil(h, m, s=0):
 a = time.localtime()
 at = time.mktime(a)
 print "now: ", a
 b = time.struct_time((a[0], a[1], a[2], h, m, s, 0, 0, a[-1]));
 bt = time.mktime(b)
 print "until: ", b
 diff = bt-at
 print "seconds to wait: ", diff
 wait(diff)

sleepUntil(14, 30)
click(<some-image>)
# end of copy/paste block

Copy the lines from # start to # end (include them, no problem a # means a comment) and paste them into the IDE. 
press run and it should work. To not have to wait until 14:30 take a time 1 or 2 minutes away and write e.g.
sleepUntil(17, 56) # now it is 17:54 my local time, sit back and wait for the click;-)

You might notice, that the indentation is one blank - no problem if you leave it this way.
The problem is, if you paste scripts here in launchpad, tabs are converted to blanks. That is usually not a problem, until you start to modify the code in the IDE and then it may happen, that blank and tab indents are mixed and might produce errors, that are hard to find. So again the rule: use always tabs for indentation.

If you want to learn, how to fix indentations: (after pasting the code to the IDE):
- select all lines from " a = time.localtime()" to " wait(diff)" inclusive
- press shift-tab until all lines are left justified
- press tab one time, which implements the correct indentation.

This is the general approach to fix indentation problems, either for one
line or for a bunch of lines.

Have fun with Sikuli.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.