Run def from another def

Asked by Steen

Hi

when having a not exist of an png can i make the def run another def?

def Run_app
       switchapp("CMD")
       if not exist("CMD.png",5)
         run def Rerun_app

def Rerun_app
          type(Key.ESC, KeyModifier.CTRL)
          wait(2)
          paste("C:\Windows\system32\cmd.exe\n")

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
TestMechanic (ndinev) said :
#1

Yes you can, but be careful with syntax of python. Your example is not correct "run def Rerun_app". You need just to call the function.

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

What dinev wanted to say:
Defining / calling a function always should have / needs a pair of brackets, even if they are empty.
Mind the colon needed on the def line.
Rerun_app has to be first.

def Rerun_app():
          type(Key.ESC, KeyModifier.CTRL)
          wait(2)
          paste("C:\Windows\system32\cmd.exe\n")

def Run_app():
       switchapp("CMD")
       if not exist("CMD.png",5)
         run def Rerun_app()

your string with paste contains \'s, so you have to use raw string or double the backslashes::

paste(r"C:\Windows\system32\cmd.exe" + "\n")

or

paste("C:\\Windows\\system32\\cmd.exe\n")

or even:

paste(r"C:\Windows\system32\cmd.exe"); type(Key.ENTER))

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

Sorry another bug:

run def Rerun_app()

has to be just (as dinev told already ;-)

Rerun_app()

Revision history for this message
Steen (steenc) said :
#4

Thanks RaiMan, that solved my question.