← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #216473]: Loop through list

 

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

Description changed to:
Hello,

Before I begin, I just want to let it be known that I have read FAQ
1437, and even look up Python tutorials elsewhere; however, I'm stuck.
I'm completely new to programming/scripting.

# --- loop through a list
myListFixed = (11, 22, 33, 44)
for entry in myListFixed:
   print entry
   lastEntry = entry
   while not exists(img, 0):
      wait(1) # we wait for the entry field
   type(getLastMatch(), e+Key.ENTER)
   wait(3)
   # now the next entry
print "the last entry was:", lastEntry

Using the above example, when I try to run the script, I received an
error that states "e" is not defined. I even tried "entry+Key.ENTER"
without luck.

My problem: I have a list that I would like to be typed in sikuli.
First, the script will click a few buttons, then a text field, and then
enter the 1st item on the list, press enter.  Then, repeat from step 1,
with the exception that 2nd item will be entered this time instead of
the first item.

For example:

click(".png") # Step 1, Button
click(".png") # Text field
myListFixed = (11, 22, 33, 44) 
for entry in myListFixed:
      lastEntry = entry
   while not exists(img, 0):
      wait(1) # we wait for the entry field
   type(getLastMatch(), e+Key.ENTER)  # I would like to loop through myListFixed
   wait(3)
Restart from step 1 # now the next entry

While we are at loop and list, it is possible to loop array such as
this:

List1= [1,2,3]
List2= [a,b,c]

And to have sikuli type "1" in the first text field, and "a" in the
second text field, then repeat the process entering "2","b", etc...

Thank you very much for your help.  In the meantime I will continue to
search for the answers on the board.

UPDATE:  With the script below, I managed to get the loop going but it
only loop/repeat "1"

myList = ["1","2","3","4","5"]
for entry in myList:
    while not exists("SikuliCreate.png",0):
        wait(3)
        type(getLastMatch(),entry+Key.ENTER)
        wait(3)
    
Update 2:

I finally got it! By combining this question (
https://answers.launchpad.net/sikuli/+question/194613) and FAQ 1437, and
some youtube video, LOL. The code came out to be this:

For imported file:

\import os
loginsList = [] # empty list/array
dir = "d:/" # the folder, where your ids.txt is
loginsFile = os.path.join(dir, "file.txt") # makes a Windows filename
loginsFile = open(loginsFile) # opens ids file
for line in loginsFile.readlines(): # reads line by line
    logins = line.strip() # get rid of newline
    print logins
    loginsList.append(logins) # add id to list/array
loginsFile.close() # close file
for logins in loginsList:
    type(logins)

For basic list loop (continuously repeating the list):
myList = ["1","2","3","4","5"]
while not exists("S.png",0):
    for entry in myList: 
        switchApp (blah.exe)
        wait(1)
        type(entry+Key.ENTER) #type into notepad
        wait(1)

-----------------
I'm not sure how's my script is slightly different from the one in the instruction from FAQ 1437 but it seems to do what I need. The continuous loop of the first item in the list seem to be caused by the while-not-exist being below the for-in entry. I'm posting it here so others can take a look at it. 

Please mark this solved/answered.  Thank you.

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