Jump to content

How to make an au3-code-Test-Tool by using 'AutoIt3.exe' ?


Recommended Posts

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
Global $oMyError,$oErr

TestAu3Code()

Func TestAu3Code()
    
    GUICreate("CodeTest")
    Local $idEdtCode = GUICtrlCreateEdit("", 10, 10, 380, 180, $ES_AUTOVSCROLL + $WS_VSCROLL)
    Local $idCommand = GUICtrlCreateButton("RunCode", 310, 200, 80, 25)
    Local $idEdtEror = GUICtrlCreateEdit("", 10, 235, 380, 160, $ES_AUTOVSCROLL + $WS_VSCROLL)
    GUISetState(@SW_SHOW)
    
    GUICtrlSetData($idEdtCode, 'MsgBox(4096, "", "_IEPropertyGet($oIE, "locationurl")')

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idCommand
                $oMyError = ObjEvent("AutoIt.Error", "ErrFunc")
                Local $oIE = _IECreate("www.baidu.com")
               ;ShellExecute(@AutoItExe, GUICtrlRead($idCommand))
            Execute(GUICtrlRead($idCommand))     
                If @error Then GUICtrlSetData($idEdtEror,"ShellExecute @error = " & @error & @CRLF, 1)
                Local $sError=ErrFunc($oErr)
                If $sError Then GUICtrlSetData($idEdtEror, @CRLF & "AutoIt.Error : " & @CRLF & $sError & @CRLF, 1)
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>TestAu3Code
Func ErrFunc($oError)
    Return "Catch a COM Error !" & @CRLF & _
            "ErrCode: 0x" & Hex($oError.number, 8) & @CRLF & _
            "ErrDesc: " & $oError.windescription & _
            "ErrLine: " & $oError.scriptline
EndFunc   ;==>ErrFunc

The code is as above.

It Can run but always pop up msgbox miserror.

How can make it work more smoothly?

Thanks ahead!

Edited by Letraindusoir
Link to comment
Share on other sites

Well there is quite a number of problems with your code.  The biggest issue is the fact that when you reshell autoit (shellexecute or run), it becomes a new process thus a completely new script.  There is no link between the caller and the callee (unless you add command line parameters or use some other means like IPC).  To get a good example of how you can reshell autoit on a single line of code, see help file / Using AutoIt / Command Line Parameters.

Edited by Nine
Link to comment
Share on other sites

  • Letraindusoir changed the title to 如何使用'AutoIt3.exe'制作au3-code-Test Tool?
  • Letraindusoir changed the title to How to make an au3-code-Test-Tool by using 'AutoIt3.exe' ?
On 8/31/2021 at 1:59 AM, Nine said:

Well there is quite a number of problems with your code.  The biggest issue is the fact that when you reshell autoit (shellexecute or run), it becomes a new process thus a completely new script.  There is no link between the caller and the callee (unless you add command line parameters or use some other means like IPC).  To get a good example of how you can reshell autoit on a single line of code, see help file / Using AutoIt / Command Line Parameters.

thanks,Nine

The effect I want to achieve is to update the Au3 code in the editbox and then run the changed code without having to re-open the master program.

It doesn't seem to work with Cmdline and maybe I don't know how to use cmdline yet

Link to comment
Share on other sites

Maybe this ?

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

Global $oMyError, $idEdtError, $Test

TestAu3Code()

Func TestAu3Code()

  GUICreate("CodeTest")
  Local $idEdtCode = GUICtrlCreateEdit("", 10, 10, 380, 180, $ES_AUTOVSCROLL + $WS_VSCROLL)
  Local $idCommand = GUICtrlCreateButton("RunCode", 310, 200, 80, 25)
  $idEdtError = GUICtrlCreateEdit("", 10, 235, 380, 160, $ES_AUTOVSCROLL + $WS_VSCROLL)
  GUISetState(@SW_SHOW)

  GUICtrlSetData($idEdtCode, 'Assign("Test", MsgBox(4096, "", _IEPropertyGet($oIE, "locationurl")))')

  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
      Case $idCommand
        $oMyError = ObjEvent("AutoIt.Error", "ErrFunc")
        Local $oIE = _IECreate("www.baidu.com")
        Execute(GUICtrlRead($idEdtCode))
        If @error Then GUICtrlSetData($idEdtError, "Execute @error = " & @error & @CRLF, 1)
        ConsoleWrite("Resulting = " & $Test & @CRLF)
    EndSwitch
  WEnd
EndFunc   ;==>TestAu3Code

Func ErrFunc($oError)
  GUICtrlSetData($idEdtError, "Catch a COM Error !" & @CRLF & _
      "ErrCode: 0x" & Hex($oError.number, 8) & @CRLF & _
      "ErrDesc: " & $oError.windescription & @CRLF & _
      "ErrLine: " & $oError.scriptline & @CRLF, 1)
EndFunc   ;==>ErrFunc

 

Link to comment
Share on other sites

22 hours ago, Nine said:

Maybe this ?

I'm sorry

Because I am not good at English, maybe my expression is not easy to understand.

I would like to do a IE test form.

After declaring a global variable "$oIE",

Input the code in the code-editbox and then click the "RunCode" button to see the running results.

In this way,

by constantly updating the code in the code-editbox,

we can easily continuously view the code running results.

I rewrote the code to help to understand:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Array.au3>

Global $oIE,$oMyError, $idEdtEror,$Result

$oIE = _IECreate()

TestAu3Code()

Func TestAu3Code()

    GUICreate("CodeTest")
    Local $idEdtCode = GUICtrlCreateEdit("", 10, 10, 380, 160, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
    Local $idCommand = GUICtrlCreateButton("RunCode", 310, 180, 80, 25)
    $idEdtRetn = GUICtrlCreateEdit("", 10, 215, 380, 120, $ES_AUTOVSCROLL + $WS_VSCROLL)
    $idEdtEror = GUICtrlCreateEdit("", 10, 345, 380, 50, $ES_AUTOVSCROLL + $WS_VSCROLL)
    GUISetState(@SW_SHOW)
    
    Local $code = ""
    $code &= '_IENavigate($oIE,"https://www.autoitscript.com/forum/")' & @CRLF
    $code &= '_IEPropertyGet($oIE, "locationurl") ' & @CRLF
    $code &= 'Local $oKeyword=_IEGetObjById($oIE,"elSearchField")' & @CRLF
    $code &= '$oKeyword.value="excute"'

    GUICtrlSetData($idEdtCode, $code)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idCommand
                $oMyError = ObjEvent("AutoIt.Error", "ErrFunc")
                Local $aCode = StringSplit(GUICtrlRead($idEdtCode), @CRLF, 1)
                _ArrayDisplay($aCode)
                For $i = 1 To $aCode[0]
                    If StringLen(StringStripWS($aCode[$i], 8)) Then
                        $Result = Execute($aCode[$i])
                        If @error Then GUICtrlSetData($idEdtRetn, "Execute @error = " & @error & @CRLF, 1)
                        GUICtrlSetData($idEdtRetn, "Result = " & $Result & @CRLF, 1)
                    EndIf
                Next
        EndSwitch
    WEnd
EndFunc   ;==>TestAu3Code

Func ErrFunc($oError)
    GUICtrlSetData($idEdtEror, "Catch a COM Error !" & @CRLF & _
            "ErrCode: 0x" & Hex($oError.number, 8) & @CRLF & _
            "ErrDesc: " & $oError.windescription & @CRLF & _
            "ErrLine: " & $oError.scriptline & @CRLF, 1)
EndFunc   ;==>ErrFunc

 

Edited by Letraindusoir
Link to comment
Share on other sites

5 hours ago, junkew said:

Seems like you are writing a helpertool but i do not yet see the benefit over just using scite and run button.

I just want to be easier when testing operating web-page by way of Au3

so want to run au3 code directly with Au3 as with scite.

@AutoItExe,execute all used and didn't work well

I didn't know too much about it, and I was stumped

 

Link to comment
Share on other sites

Did you read the note in the help file
Note that either Call() or the called function can set the @error flag. If Call() sets the @error flag, the value will be 0xDEAD and @extended will also be set to 0xBEEF. See the example for a demonstration of testing for a function that was not found.

I only see 3 solutions to do what you want but probably its not easy and leads to writing your own lexer/parser

1. Mix the call, assign, execute and eval functions

call("_IENavigate",$oIE,"https://www.autoitscript.com/forum/")
consolewrite("Execute @error = " & @error & @CRLF)

I think the assign, execute and eval where not made to also call functions although I am not sure. 

As you can see this does not work (and as such your coding neither)

execute('_IENavigate($oIE,"https://www.autoitscript.com/forum/"')
consolewrite("Execute @error = " & @error & @CRLF)

see example at bottom based on your code extended with consolewrite, msgbox as a base starting point

2. Make use of a function variable but will lead to similar things as in point 1

$f=_IENavigate

$f($oIE,"https://www.autoitscript.com/forum/")

3. write out the textbox content to a modified .au3 file and execute that

for point 2 this temp output could look like below (but probably will be more complicated)

''## put default includes here

for $i=0 to 3
    runline($i)
    if @error then ....
next

function runLine($lineNo)  
    switch $lineNo
        case 0
            _IENavigate($oIE,"https://www.autoitscript.com/forum/")
        case 1
            _IEPropertyGet($oIE, "locationurl") 
        case 2
            Local $oKeyword=_IEGetObjById($oIE,"elSearchField")
        case 3
            $oKeyword.value="excute"
    endswitch
end function

Example

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Array.au3>

Global $oIE,$oMyError, $idEdtEror,$Result

$oIE = _IECreate()
sleep(2000)
;call("_IENavigate",$oIE,"https://www.autoitscript.com/forum/")
;consolewrite("Execute @error = " & @error & @CRLF)
;consolewrite(isdeclared("$oIE"))
;consolewrite(isdeclared("oIE"))

;execute('_IENavigate($oIE,"https://www.autoitscript.com/forum/"')
;consolewrite("Execute @error = " & @error & @CRLF)

;call('consolewrite','"https://www.autoitscript.com/forum/"')
;consolewrite("Execute @error = " & @error & @CRLF)

;call('MsgBox', 0, "", "hello world")
;If @error = 0xDEAD And @extended = 0xBEEF Then consolewrite("Wrong function or parameters " & @CRLF)
;local $aArgs[4] =["CallArgArray", 0,"","hello world"]
;call('MsgBox',$aArgs)
;If @error = 0xDEAD And @extended = 0xBEEF Then consolewrite("Wrong function or parameters " & @CRLF)

TestAu3Code()

Func TestAu3Code()

    GUICreate("CodeTest")
    Local $idEdtCode = GUICtrlCreateEdit("", 10, 10, 380, 160, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
    Local $idCommand = GUICtrlCreateButton("RunCode", 310, 180, 80, 25)
    $idEdtRetn = GUICtrlCreateEdit("", 10, 215, 380, 120, $ES_AUTOVSCROLL + $WS_VSCROLL)
    $idEdtEror = GUICtrlCreateEdit("", 10, 345, 380, 50, $ES_AUTOVSCROLL + $WS_VSCROLL)
    GUISetState(@SW_SHOW)

    Local $code = 'consolewrite("Hello world")' & @CRLF
    $code &= 'msgbox(0,"","hello world")' & @CRLF
    $code &= '_IENavigate($oIE,"https://www.autoitscript.com/forum/")' & @CRLF
    $code &= '_IEPropertyGet($oIE, "locationurl") ' & @CRLF
    ;$code &= 'Local $oKeyword=_IEGetObjById($oIE,"elSearchField")' & @CRLF
    ;$code &= '$oKeyword.value="excute"'

    GUICtrlSetData($idEdtCode, $code)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idCommand
                $oMyError = ObjEvent("AutoIt.Error", "ErrFunc")
                Local $aCode = StringSplit(GUICtrlRead($idEdtCode), @CRLF, 1)
                local $myParams[1]
                ;_ArrayDisplay($aCode)
                For $i = 1 To $aCode[0]
                    If StringLen(StringStripWS($aCode[$i], 8)) Then
                        ;'$Result = Execute($aCode[$i])
                        $splitParts=StringSplit($aCode[$i],"(,)")
                        $fName=$splitParts[1]
                        if (stringinstr($aCode[$i],"=")>0) Then
                            consolewrite("its an assignment, you have to do a little more " & @CRLF )
                        Else
                            consolewrite($splitParts[0] & @CRLF )
                            _ArrayDisplay($splitParts)
                            $parCount=$splitParts[0] - 2
                            redim $myParams[$parCount+1]
                            $myParams[0] = "CallArgArray"

                            ;Evaluate the parameter before we throw it into the call
                            for $j=2 to $parCount+1
                                ;If not a string
                                if stringleft(StringStripWS($splitParts[$j],$STR_STRIPLEADING + $STR_STRIPTRAILING),1)<>'"' Then
                                    if isnumber($splitParts[$j]) then
                                        consolewrite("Evaluating number " & $splitParts[$j] &@CRLF )
                                        $myParams[$j-1]=int($splitParts[$j])
                                    Else
                                        local $parName=stringreplace($splitParts[$j],"$","")
                                        consolewrite("Evaluating param " & $parName & isdeclared($parName) & @CRLF )
                                        $myParams[$j-1]=eval($parName)
                                    endif
                                Else
                                    consolewrite("Copying " & $splitParts[$j] & @CRLF )
                                    $myParams[$j-1]=$splitParts[$j]
                                EndIf
                            next
                            _ArrayDisplay($myParams)
                            consolewrite("Calling " & $parCount & $fName & $myParams[0] & $myParams[1] & @CRLF )
                            call($fName, $myParams)
                            If @error = 0xDEAD And @extended = 0xBEEF Then consolewrite("Wrong function or parameters " & @CRLF)

                        EndIf

                        If @error Then GUICtrlSetData($idEdtRetn, "Execute @error = " & @error & @CRLF, 1)
                        GUICtrlSetData($idEdtRetn, "Result = " & $Result & @CRLF, 1)
                    EndIf
                Next
        EndSwitch
    WEnd
EndFunc   ;==>TestAu3Code

Func ErrFunc($oError)
    GUICtrlSetData($idEdtEror, "Catch a COM Error !" & @CRLF & _
            "ErrCode: 0x" & Hex($oError.number, 8) & @CRLF & _
            "ErrDesc: " & $oError.windescription & @CRLF & _
            "ErrLine: " & $oError.scriptline & @CRLF, 1)
EndFunc   ;==>ErrFunc

 

Link to comment
Share on other sites

16 hours ago, junkew said:

Did you read the note in the help file
Note that either Call() or the called function can set the @error flag. If Call() sets the @error flag, the value will be 0xDEAD and @extended will also be set to 0xBEEF. See the example for a demonstration of testing for a function that was not found.

I only see 3 solutions to do what you want but probably its not easy and leads to writing your own lexer/parser

junkewSorry to trouble you and great thanks for your hard work!

I know nothing about au3 compiler.

I originally thought au3 code could be run with @AutoItExe,execute,eval and so on.

It seems that  I thought it was too simple.

so ,Leave this problem to be solved later.

I would honestly test the code with scite......

thanks again!

Edited by Letraindusoir
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...