Sign in to follow this  
Followers 0
Imbuter2000

execute a piece of (multiline) code read from a text area?

9 posts in this topic

I need to read a piece of code (multiple lines of free code) from a text area (GUICtrlCreateEdit) in GUI and execute it.

Is it possible (without saving it as au3 and running it)? if so, how?

Share this post


Link to post
Share on other sites



It looks like it can be done with /AutoIt3ExecuteLine. I have been working with inserting an input line to the code to give you a decent example, but I have not been successful in making it work. I think my trouble is getting the single and double quotes correct. Here is the example from the help file:

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

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

It looks like it can be done with /AutoIt3ExecuteLine. I have been working with inserting an input line to the code to give you a decent example, but I have not been successful in making it work. I think my trouble is getting the single and double quotes correct. Here is the example from the help file:

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

Thanks for the suggestion... but what if they are multiple lines of code? and... important... in that code can I use variables declared in the parent code or declare variables that are then used in the parent code?

I need it to execute it as if it were a part of the parent code...

Share this post


Link to post
Share on other sites

Hi, Imbuter2000. That is a lot of "what-ifs". Can you give an example of the text you'd be trying to pull in and execute?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <File.au3>
Global $PID

$hGui = GUICreate('My Program', 300, 260)
$iStart = GUICtrlCreateButton('Start', 10, 230, 120, 22)
$iStop = GUICtrlCreateButton('Stop', 140, 230, 120, 22)
$iEdit = GUICtrlCreateEdit('', 10, 10, 280, 180)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $iStart
            $PID = _ExecuteFile(GUICtrlRead($iEdit))
        Case $iStop
            If ProcessExists($PID) Then ProcessClose($PID)
        Case -3
            Exit
    EndSwitch
WEnd

Func _ExecuteFile($sText)
    Local $hFile, $pid, $s_TempFile
    $s_TempFile = _TempFile()
    $hFile = FileOpen($s_TempFile, 2)
    FileWrite($hFile, $sText)
    FileClose($hFile)
    $pid = Run('"' &amp; @AutoItExe &amp; '" /AutoIt3ExecuteScript "' &amp; $s_TempFile &amp; '"')
    Sleep(300)
    FileDelete($s_TempFile)
    Return $pid
EndFunc

Edited by AZJIO

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

AZJIO: thanks but I wrote "without saving it as au3 and running it" for 2 reasons:

- because in that user-defined code I want to use variables declared in the parent code and/or declare variables that are then used in the parent code

- because I want to execute it in an endless fast loop so that I see the results while I type the code, and using a file would speed down the loop...

JLogan3o13: an example of user-defined code is this:

[/color][/font]

$regex_before = "<a" _
& myfunction1("style","color:s*red") _
& myfunction1("class","arial24") _
& myfunction1("href","/user/.+?") _
& myfunction2(3)
$regex_grab = grab($regex_before)
$regex_after = "</as*>"
$finalregex = "(?s)" & $regex_before & $regex_grab & $regex_after

The parent app contains the functions myfunction1, myfunction2 and grab and must continually (in an endless loop) test $finalregex over an html page showing the results in a side area of the same GUI where this user-defined code is written...

Edited by Imbuter2000

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Imbuter2000

I'll be busy for several hours. This was done while I was free.

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$Size = 8
Global $Disk = 'Q:'
RunWait(@ComSpec & ' /C ' & @SystemDir & 'imdisk.exe -a -s ' & $Size & 'M -m ' & $Disk & ' -p "/fs:ntfs /y /q /v:imdisk', '', @SW_HIDE)
$s_TempFile = $Disk & 'file.au3'
FileCopy(@AutoItExe, $Disk & '1.exe', 9)
Global $PID, $Toggle

$hGui = GUICreate('My Program', 300, 260)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$iStart = GUICtrlCreateButton('Start / Stop', 10, 230, 120, 22)
GUICtrlSetOnEvent(-1, "_Toggle")
$iEdit = GUICtrlCreateEdit('', 10, 10, 280, 180)
GUISetState()
While 1
    If $Toggle Then
        _ExecuteFile(GUICtrlRead($iEdit))
    EndIf
    Sleep(30)
WEnd

Func _Toggle()
    $Toggle = Not $Toggle
    If Not $Toggle And ProcessExists($PID) Then ProcessClose($PID)
EndFunc

Func _ExecuteFile($sText)
    If Not $sText Then
        _Toggle()
        Return
    EndIf
    Local $hFile
    If ProcessExists($PID) Then ProcessClose($PID)
    $hFile = FileOpen($s_TempFile, 2)
    FileWrite($hFile, $sText)
    FileClose($hFile)
    $PID = Run($Disk & '1.exe /AutoIt3ExecuteScript ' & $s_TempFile)
EndFunc

Func _Exit()
    RunWait('rundll32.exe imdisk.cpl,RunDLL_RemoveDevice ' & $Disk, '', @SW_HIDE)
    Exit
EndFunc
Edited by AZJIO

Share this post


Link to post
Share on other sites

Imbuter2000

I'll be busy for several hours. This was done while I was free.

#include <GUIConstantsEx.au3>
...
I tried it with the following code but nothing happens:

$a = "one"
msgbox(0,"",$a)

and when I close it it gives an error saying that it can't find the file imdisk.cpl

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

and when I close it it gives an error saying that it can't find the file imdisk.cpl

Google for what? imdisk

I tried it with the following code but nothing happens:

You say to update the data. It is not intended to stop the script. Any msgbox will be killed the next step cycle (loop) of 30 milliseconds. In your answers, I realized that you still too early to use this script to solve problems. Edited by AZJIO

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