Jump to content
Sign in to follow this  

New to AutoIt, not understanding ConsoleRead/Write function

Recommended Posts


"Console Read: Read from the STDIN stream of the AutoIt script process"
I'm not sure what the STDIN stream is but my guess is it's a stream which tracks what happens when I run a script. Like a log file.
However, I can not wrap my mind around how these two functions work: ConsoleRead, ConsoleWrite
Let me illustrate with an example:

Local $var=consolewrite("hi")
MsgBox(1, "", $var)

This is working perfectly fine.

Local $var2=consoleread()
consolewrite("hi") ;with this I thought I wrote "hi" to the console, which I will then be available to read with consoleread.
MsgBox(1, "", $var2)

This just returns a blank msgbox.

Did I not write "hi" to the console or does consoleread not read what's in the console?

Sidequestion: How do you all find out what's going wrong with your scripts? Read the console at bottom of autoit? All I see there is "Exit code: 0" which doesn't help me much. Anywhere I can find a list of exitcodes and their meaning?

Slightlyrelated to first sidequestion: is the value of  the @error macro same as the consoles exitcode? If not, do you ppl. always send @error in a msgbox to see what went wrong, or is there another way of doing this?

Thanks on beforehand


Edited by sackjarrow

Share this post

Link to post
Share on other sites

ConsoleWrite will normally output some text (ie. some variables you want to debug) at the bottom of the SciTE (if you're using that).  If you compile it into an .EXE, it does nothing (unless you have a program that will specifically look for console output, but that's more advanced).
ConsoleRead is the exact opposite of ConsoleWrite.  It looks for console output and returns that to you as a string.  I honestly have never used this function, but I have done *lots* of debugging.  I don't think ConsoleRead is useful expect when you want to grab the text output of another console application and redirect it to your script.  See the help file on ConsoleRead for more information.  It's pretty specific-use, and most people don't need it.
"Exit Code 0" means that your script terminated without errors.  It only means it didn't crash on you.  It's nice to know, but it really doesn't help you nail down where an error is coming from.  Exit codes can be found by searching the help file for "exit code", or go to the Contents tab -> AutoIt -> Using AutoIt -> AutoIt Syntax Checker (Au3Check).

Windows maintains an internal variables called Error and Extended for every running program.  In AutoIt, they're called @error and @extended.  They're simply a record of any current error-state.  Any function that can set error values has a description in the helpfile for what the values mean.  For example, if you're using StringRegExp incorrectly (with Flag = 0), @error would be 2 (meaning you handed the function an invalid pattern), and @extended would tell you where your pattern is broken.

There's lots of ways to debug a program.  Ultimately, you generally want to know what values your variables have at different points in the script, and that helps you find out why it's not working.  I sometimes use a MsgBox displaying variable values at different points in my script.  It halts the script while it shows up, which can be useful.  Sometimes I use a ToolTip if I'm working with a GUI that has values changing based on user interaction.  If I just want to know the value of a variable when the script hits a certain point, I'll use ConsoleWrite to output that.

Hope this was helpful!

EDIT - Forgot to talk about STDIN/STDOUT.

STDIN/STDOUT (Standard In & Standard Out) are concepts relating to application input/output.  In Windows, any application (typically console applications) can send output to the STDOUT stream.  The STDOUT stream is short-hand for "whatever default output method is currently selected".  The same goes for STDIN - applications can get input from it, and it's whatever the default is.  The STDIN/STDOUT can be configured to be many different things, but it's mostly used for low-level console applications.  It's not directly useful to me personally except for using ConsoleWrite to check on my variables.

When you run an .au3 script from within SciTE, SciTE registers itself as the default stream for your script.  That way, the STDOUT text (ConsoleWrite) gets redirected to SciTE and you can see the output.  Confused?  I don't really get it myself.  Like I said, it works with ConsoleWrite, and that's all I need it to do.  ^_^

Edited by Artisan

Share this post

Link to post
Share on other sites


Just refer to the help file and have a look through the functions you are after.  There's a guide on the downloads section called AutoIT 1-2-3 which is pretty good at starting you off and is accompanied by a walkthrough GUI interface ('?do=embed' frameborder='0' data-embedContent>>)

Below is an incredibly simple use of a consolewrite.  I use consolewrites and messagebox to debug a lot of my code when I'm first writing so I can check the variables are what I think they are.

; Define your variable as below then use consolewrite to display it within the console.

Local $var = "hello"
consolewrite( $var & @CR )

; display the variable within a message box

msgbox( 0, "Test Box", $var)

If you write yourself a simple statement, assign it a variable and then call a consolewrite as I've done above after it to ensure the result is as you expected.



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