Jump to content

Function "Execute" fails to execute string from STDIN in console mode


Go to solution Solved by mistersquirrle,

Recommended Posts

My idea is to run functions interactively from a console window. But "Execute" can never execute, lol. 


Global $sStdIn, $sExecReturn

While 1

    $sStdIn = ConsoleRead()

    If $sStdIn <> "" Then
        $sExecReturn = Execute($sStdIn)
        If @error Then
            MsgBox(16, "Error", "Cannot execute: " & $sStdIn)





If I add quotes like this:

Execute('"' & $sStdIn & '"')

then "Execute" just returns $sStdIn without executing.


Also, if I compile it in GUI mode and pipe STDIN of this script to STDOUT of another non-console application, then everything works as expected.

What I'm doing wrong?

Link to comment
Share on other sites

  • Solution

I think similar to 

Part of the problem is how the 'Console' works in an uncompiled script. You're running it through the AutoIt.exe process, not through SciTE or its own process. You're not inputting into the 'correct' console window, is basically my thought. 

So, basically, you cannot do what you want unless you compile the script, and do you things from the console window of the direct .exe file. As for why Execute is not working, that's because of CR/LF/CRLF, basically. I tested your code, and some example code built from the help file entry for Execute, and I got it working:

#include <MsgBoxConstants.au3>
    Compile this script to "ConsoleRead.exe".
    Open a command prompt to the directory where ConsoleRead.exe resides.
    Type the following on the command line:
    echo Hello! | ConsoleRead.exe

    When invoked in a console window, the above command echos the text "Hello!"
    but instead of displaying it, the | tells the console to pipe it to the STDIN stream
    of the ConsoleRead.exe process.

; MouseClick("main", 436, 451, 1, 10) ; Copy this and try executing that, or whatever you want

Func Example()
    If Not @Compiled Then
        MsgBox($MB_SYSTEMMODAL, "", "This script must be compiled in order to run the example.")

    Local $sOutput
    While True
        $sOutput = ConsoleRead()
;~      If @error Then ExitLoop

        If $sOutput <> '' Then
            MsgBox($MB_SYSTEMMODAL, "", "Received: " & @CRLF & 'Type: ' & VarGetType($sOutput) & @CRLF & $sOutput)
            Execute(StringStripCR(StringStripWS($sOutput, 1 + 2))) ; $STR_STRIPLEADING + $STR_STRIPTRAILING
            If @error Then
                MsgBox($MB_SYSTEMMODAL, "", "Execute $sOutput error: " & @CRLF & @CRLF & @error)
    MsgBox($MB_SYSTEMMODAL, "", "Received: " & @CRLF & @CRLF & $sOutput)
EndFunc   ;==>Example

Give that a shot (compiled) and see if it works for you, it did for me (sending the MouseClick line in there). Keep in mind then that this will only work for single line things.


Edit: Also take a look at https://www.autoitscript.com/autoit3/docs/intro/running.htm, specifically: AutoIt specific command Line Switches


Run a single line of code:

AutoIt3.exe [/ErrorStdOut] /AutoIt3ExecuteLine "command line"
                Execute one line of code.

The command below will execute that single line of code and display the MsgBox with "Hello World!". The tray icon will not be displayed. 

@@SyntaxHighlighting@@ Run(@AutoItExe & ' /AutoIt3ExecuteLine "MsgBox(4096, ''Hello World!'', ''Hi!'')"') @@End@@
Run a script using another compiled script:

Compiled.exe [/ErrorStdOut] /AutoIt3ExecuteScript file [params ...]
                Execute another AutoIt script file from a compiled AutoIt3 executable. 

Compiled.exe [/ErrorStdOut] /AutoIt3ExecuteLine "command line"
                Execute one line of code as with AutoIt3.exe above. 

And keep in mind that to use AutoIt3ExecuteScript  or AutoIt3ExecuteLine, the file needs to be compiled with this flag: 

#pragma compile(AutoItExecuteAllowed, true)


Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

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

  • Recently Browsing   0 members

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