Read lines from a file and pause for hotkey?

Asked by Julian Moorhouse

I'd like to iterate through lines in from a file. Using each line to paste or type that line into an application.
I'd then like to wait for some interaction perhaps via a hotkey.

I've managed to find this example code to read through a file.
Also I've found how to handle a hotkey with a while loop

Putting it all together I have something like...

running = true
def runHotkey(event):
 global running
 running = false

Env.addHotkey(Key.NUM4,0, runHotkey)

theFile = file(r"F:\Office\Script\sikulitestfiles\Text.txt")
for line in theFile:
  while running:
      print line # print line in sikuli
      click("1330680438453.png"), type(line)
theFile.close()

Does this look ok / safe ?

Thanks in advance.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

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

for line in theFile:
  while running:
      print line # print line in sikuli
      click("1330680438453.png"), type(line)

will not work, since the while loop will repeat with the first line until running gets False.

for line in theFile:
  if running: # check wether hotkey was pressed
      print line # print line in sikuli
      click("1330680438453.png")
      wait(0.5) # this depends on how fast your GUI reacts
      type(line) # paste() is needed if line contains special characters or even non-ASCII
      wait(5) # see comment

comment: when a line is processed, the script should wait some time to give you a chance to observe and react ;-)

without

Revision history for this message
Julian Moorhouse (mindwarpjules) said :
#3

Oops I didn't mean to mark that as answer.

Thanks, however I really need to wait until a hotkey is pressed rather than wait for a fixed period.

Revision history for this message
Julian Moorhouse (mindwarpjules) said :
#4

Hmmm I could perhaps but the hotkey check around the pause

Revision history for this message
Manfred Hampl (m-hampl) said :
#5

What exactly do you want? Should the process wait for a key-press after each line?

In my opinion in such case some kind of popup would be a better choice than a hotkey, see http://sikulix-2014.readthedocs.io/en/latest/interaction.html

The hotkey feature better serves the requirements of interrupting the processing wherever it might be at that moment. What you need seems to be a decision to wait or continue at a specific point in the processing.

Revision history for this message
Julian Moorhouse (mindwarpjules) said :
#6

>Should the process wait for a key-press after each line?

Yes.

A popup sounds ideal :)

Revision history for this message
Julian Moorhouse (mindwarpjules) said :
#7

To make things perfect, could I somehow exit the file loop. I'm thinking I could use an input popup or a yes / no popup.

Thoughts ?

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

instead of popup use popAsk(), which has a yes/no choice.
see:
http://sikulix-2014.readthedocs.io/en/latest/interaction.html#popAsk

Revision history for this message
Julian Moorhouse (mindwarpjules) said :
#9

Sorry I actually meant, how would I exit the line for loop.
If there and exitfor perhaps ?

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

SikuliX talks Python ;-)

break

jumps out of a loop

Can you help with this problem?

Provide an answer of your own, or ask Julian Moorhouse for more information if necessary.

To post a message you must log in.