Sign in to follow this  
Followers 0
kzar

Function that takes code as parameter?

6 posts in this topic

I was wondering is it possible to make a function that takes autoit code as a parameter and then runs that code? I am trying to get rid of a lot of duplication in my script. Thanks

Share this post


Link to post
Share on other sites



What do you mean if you say "run code" ?

AutoIt3.exe [/ErrorStdOut] /AutoIt3ExecuteLine "command line"

Or

Sample ('calc.exe')
Sample ('notepad.exe')
Sample ('cmd.exe')

Func Sample ($a)
    MsgBox(0, 'Sample', 'This is '&$a)
    Sleep (1000)
    MsgBox(0, 'Sample', $a&' is getting ready')
    Run ($a)
    ;....
EndFunc

Share this post


Link to post
Share on other sites

No I don't meen run a executable or somthing. In your example you pass sample a string containing the filename of the program you would like to execute. I would like it to actually treat the string as autoit code and run it.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Or does he mean...

AutoIt3.exe /AutoIt3ExecuteLine "MsgBox(0, 'My Message Box', 'Woot!')"

or from the helpfile...

Run(@AutoItExe & ' /AutoIt3ExecuteLine "MsgBox(0, ''Hello World!'', ''Hi!'')"')

?

Edited by xcal

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Cool thanks thats what I was after. I was trying to cut out code duplication in my scripts.

I have been using lod3n's idea from another topic to automate program installs reliably by looping through all the possible different windows there could be, handling them and waiting till they are gone:

run("installer.exe")
  while 1
   sleep(100)
  select
    case WinExists("Confirmation","Are you sure")
       ControlClick("Confirmation","Are you sure","Button2")
       While WinExists("Confirmation","Are you sure")
           sleep(100)
       wend
    case WinExists("Installation Complete","Installation completed succ")
        ControlClick("Installation Complete","Installation completed succ","Button1")
        While WinExists("Installation Complete","Installation completed succ")
           sleep(100)
       wend
       exitloop
   endselect
wend
exit

So here is what I have got so far, I get an error on line 4 I think because I have a string going over several lines. Anyway my question is am I on the right track here or would you do it a different way? I am trying to make reliable installer automation scripts that are as neat as possible with little duplicated code.

; This If statement would be a case in a select inside a loop normaly
If WinExists("Example window", "Welcome to the example program blah blah") Then
  HandleWin("Confirmation","Are you sure", 
          "Example window", "Welcome to the example program blah blah",
          "MsgBox(0, 'Huray', 'it worked!')
           MsgBox(0, 'Huray', 'And this!')")
EndIf

Func HandleWin($title, $text, $commands)
; Split up the commands and count
     $command = StringSplit($commands, '\n', 1)
     $commandCount = UBound($command)

; Loop through commands and execute them
     For $i = 0 to $commandCount Step +1
         Execute($command[$i])
     Next

; Wait for window to close
     While WinExists($title,$text)
           sleep(100)
     wend
     Return $value
EndFunc

Thanks

Edited by kzar

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0